This commit is contained in:
Your Name
2026-10-31 15:16:37 +08:00
commit 0eb79b43c8
16 changed files with 1491 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
+incdir+../rtl
pcie_pkg.sv
pcie5_ep_core.sv
pcie5_demo_top.sv
../tb/pcie_vhost_xtor.sv
+127
View File
@@ -0,0 +1,127 @@
// ============================================================================
// pcie5_demo_top.sv
// Emulation top level for the PCIe Gen5 vhost demo on ZeBu Server 4.
// Instantiates the endpoint core and the vhost transactor; the transactor
// drives the transaction-level TLP ports and the DUT clock.
// ============================================================================
`timescale 1ns/1ps
module pcie5_demo_top
import pcie_pkg::*;
(
);
logic dut_clk;
logic rst_n;
// Link state driven by the vhost transactor after (virtual) link training
logic [3:0] link_speed;
logic [5:0] link_width;
logic link_up;
// Downstream TLP channel
logic req_valid, req_ready;
tlp_kind_e req_kind;
logic [63:0] req_addr;
logic [9:0] req_len_dw;
logic [127:0] req_data;
logic req_last;
logic [7:0] req_tag;
// Completion channel
logic cpl_valid, cpl_ready;
logic [127:0] cpl_data;
logic [7:0] cpl_tag;
logic cpl_last;
logic [2:0] cpl_status;
// DMA upstream channels
logic dma_req_valid, dma_req_ready;
tlp_kind_e dma_req_kind;
logic [63:0] dma_req_addr;
logic [9:0] dma_req_len_dw;
logic [127:0] dma_req_data;
logic dma_req_last;
logic [7:0] dma_req_tag;
logic dma_cpl_valid, dma_cpl_ready;
logic [127:0] dma_cpl_data;
logic [7:0] dma_cpl_tag;
logic dma_cpl_last;
logic irq_msi;
// ------------------------------------------------------------------
pcie5_ep_core u_ep (
.clk (dut_clk),
.rst_n (rst_n),
.link_speed (link_speed),
.link_width (link_width),
.link_up_in (link_up),
.req_valid (req_valid),
.req_ready (req_ready),
.req_kind (req_kind),
.req_addr (req_addr),
.req_len_dw (req_len_dw),
.req_data (req_data),
.req_last (req_last),
.req_tag (req_tag),
.cpl_valid (cpl_valid),
.cpl_ready (cpl_ready),
.cpl_data (cpl_data),
.cpl_tag (cpl_tag),
.cpl_last (cpl_last),
.cpl_status (cpl_status),
.dma_req_valid (dma_req_valid),
.dma_req_ready (dma_req_ready),
.dma_req_kind (dma_req_kind),
.dma_req_addr (dma_req_addr),
.dma_req_len_dw(dma_req_len_dw),
.dma_req_data (dma_req_data),
.dma_req_last (dma_req_last),
.dma_req_tag (dma_req_tag),
.dma_cpl_valid (dma_cpl_valid),
.dma_cpl_ready (dma_cpl_ready),
.dma_cpl_data (dma_cpl_data),
.dma_cpl_tag (dma_cpl_tag),
.dma_cpl_last (dma_cpl_last),
.irq_msi (irq_msi)
);
// ------------------------------------------------------------------
pcie_vhost_xtor u_xtor (
.dut_clk (dut_clk),
.rst_n (rst_n),
.link_speed (link_speed),
.link_width (link_width),
.link_up (link_up),
.req_valid (req_valid),
.req_ready (req_ready),
.req_kind (req_kind),
.req_addr (req_addr),
.req_len_dw (req_len_dw),
.req_data (req_data),
.req_last (req_last),
.req_tag (req_tag),
.cpl_valid (cpl_valid),
.cpl_ready (cpl_ready),
.cpl_data (cpl_data),
.cpl_tag (cpl_tag),
.cpl_last (cpl_last),
.cpl_status (cpl_status),
.dma_req_valid (dma_req_valid),
.dma_req_ready (dma_req_ready),
.dma_req_kind (dma_req_kind),
.dma_req_addr (dma_req_addr),
.dma_req_len_dw(dma_req_len_dw),
.dma_req_data (dma_req_data),
.dma_req_last (dma_req_last),
.dma_req_tag (dma_req_tag),
.dma_cpl_valid (dma_cpl_valid),
.dma_cpl_ready (dma_cpl_ready),
.dma_cpl_data (dma_cpl_data),
.dma_cpl_tag (dma_cpl_tag),
.dma_cpl_last (dma_cpl_last),
.irq_msi (irq_msi)
);
endmodule : pcie5_demo_top
+422
View File
@@ -0,0 +1,422 @@
// ============================================================================
// pcie5_ep_core.sv
// Demo PCIe Gen5 Endpoint core for ZeBu Server 4 virtual-host emulation.
//
// Transaction-level TLP interface (one beat = up to 4 DW):
// - Downstream port: requests from the virtual host (CfgRd/Wr, MemRd/Wr)
// - Completion port: read completions back to the virtual host
// - Upstream port: DMA requests issued by this endpoint + their cpls
//
// Features:
// - PCIe config space with Gen5 PCIe Capability
// (LinkCap Max Speed = 6 -> 32.0 GT/s, Max Width = x16, MSI capability)
// - BAR0: control/status register block (see README register table)
// - BAR2: 64 KB device RAM, also the DMA target buffer
// - Simple DMA engine: host memory <-> device RAM, MSI on completion
//
// NOTE: all sequential logic lives in ONE always_ff. VCS rejects variables
// (e.g. dev_ram, irq_status) driven from multiple always_ff blocks (ICPD),
// so the request FSM and the DMA FSM share a single process, and the
// helper tasks below are only called from that process.
// ============================================================================
`timescale 1ns/1ps
module pcie5_ep_core
import pcie_pkg::*;
(
input logic clk,
input logic rst_n,
// Link state reported by the virtual host (negotiated Gen5 x16)
input logic [3:0] link_speed, // 6 = 32.0 GT/s
input logic [5:0] link_width, // 16 = x16
input logic link_up_in,
// ---- Downstream TLP request port (from vhost) ----
input logic req_valid,
output logic req_ready,
input tlp_kind_e req_kind,
input logic [63:0] req_addr,
input logic [9:0] req_len_dw, // length in DW (config = 1)
input logic [127:0] req_data, // write data beat (up to 4 DW)
input logic req_last,
input logic [7:0] req_tag,
// ---- Completion port (to vhost) ----
output logic cpl_valid,
input logic cpl_ready,
output logic [127:0] cpl_data,
output logic [7:0] cpl_tag,
output logic cpl_last,
output logic [2:0] cpl_status,
// ---- Upstream DMA request port (EP as requester) ----
output logic dma_req_valid,
input logic dma_req_ready,
output tlp_kind_e dma_req_kind, // TLP_MEM_RD / TLP_MEM_WR only
output logic [63:0] dma_req_addr, // host memory address
output logic [9:0] dma_req_len_dw,
output logic [127:0] dma_req_data,
output logic dma_req_last,
output logic [7:0] dma_req_tag,
// ---- Upstream DMA completion port ----
input logic dma_cpl_valid,
output logic dma_cpl_ready,
input logic [127:0] dma_cpl_data,
input logic [7:0] dma_cpl_tag,
input logic dma_cpl_last,
// ---- Interrupt ----
output logic irq_msi // 1-cycle pulse when MSI fires
);
// ------------------------------------------------------------------------
// Device RAM: 64 KB (BAR2), 32-bit words
// ------------------------------------------------------------------------
logic [31:0] dev_ram [0:16383];
// ------------------------------------------------------------------------
// Config space software-visible state
// ------------------------------------------------------------------------
logic [15:0] cfg_command;
logic [31:0] cfg_bar0, cfg_bar1;
logic [31:0] cfg_bar2, cfg_bar3;
logic [31:0] msi_addr_lo, msi_addr_hi;
logic [15:0] msi_data;
logic msi_enable;
// ------------------------------------------------------------------------
// BAR0 registers
// ------------------------------------------------------------------------
logic [31:0] reg_scratch;
logic [31:0] reg_dma_addr_lo, reg_dma_addr_hi;
logic [31:0] reg_dma_len, reg_dma_offset;
logic reg_irq_enable;
logic reg_dma_dir; // 0 = host->device, 1 = device->host
logic irq_status_dma; // IRQ_STATUS bit0
logic rw_start_dma; // CTRL bit0 write strobe
// ------------------------------------------------------------------------
// Request FSM state
// ------------------------------------------------------------------------
typedef enum logic [1:0] { S_IDLE, S_WRITE, S_READ_CPL } state_e;
state_e state;
logic [63:0] cur_addr;
logic [9:0] beats_left; // beats remaining incl. current cpl beat
logic [7:0] cur_tag;
logic cur_is_ram; // window of the in-flight memory request
// Window decode for a fresh request (BAR2 window = device RAM)
logic addr_is_ram_now;
assign addr_is_ram_now = (cfg_bar2 != 0) &&
(req_addr[31:16] == cfg_bar2[31:16]);
// ------------------------------------------------------------------------
// DMA engine state
// ------------------------------------------------------------------------
typedef enum logic [2:0] {
DMA_IDLE, DMA_H2D_REQ, DMA_H2D_CPL, DMA_D2H_REQ, DMA_DONE
} dma_state_e;
dma_state_e dma_state;
logic [31:0] dma_bytes_left;
logic [31:0] dma_cur_host;
logic [31:0] dma_cur_dev;
logic dma_busy, dma_done;
localparam logic [7:0] DMA_TAG = 8'hA5;
assign dma_busy = (dma_state != DMA_IDLE) && (dma_state != DMA_DONE);
assign dma_req_tag = DMA_TAG;
assign req_ready = (state == S_IDLE) || (state == S_WRITE);
assign dma_cpl_ready = (dma_state == DMA_H2D_CPL);
// ------------------------------------------------------------------------
// Config space read function (one DW)
// ------------------------------------------------------------------------
function automatic logic [31:0] cfg_read(input logic [11:0] off);
unique case (off)
12'h000: cfg_read = {PCIE_DID, PCIE_VID};
12'h004: cfg_read = {16'h0010, cfg_command};
12'h008: cfg_read = 32'h0880_0001;
12'h00C: cfg_read = 32'h0000_0010;
12'h010: cfg_read = cfg_bar0;
12'h014: cfg_read = cfg_bar1;
12'h018: cfg_read = cfg_bar2;
12'h01C: cfg_read = cfg_bar3;
12'h02C: cfg_read = 32'h16C3_0001;
12'h034: cfg_read = 32'h0000_0040;
12'h03C: cfg_read = 32'h0000_0105;
// ---- PCIe Capability (offset 0x40) ----
12'h040: cfg_read = {16'h0002, 8'h70, 8'h10};
12'h044: cfg_read = 32'h0000_0821;
12'h048: cfg_read = 32'h0000_0000;
12'h04C: cfg_read = {16'h0000, 2'b00, LINK_WIDTH_X16, LINK_SPEED_GEN5,
4'h0}; // LinkCap: Gen5 x16
12'h050: cfg_read = {14'h0000, 2'b00, link_width, 2'b00, link_speed,
4'h0}; // LinkCtl/LinkSta
// ---- MSI Capability (offset 0x70) ----
12'h070: cfg_read = {16'h8000, 8'h00, 8'h05} |
(msi_enable ? 32'h0001_0000 : 32'h0);
12'h074: cfg_read = msi_addr_lo;
12'h078: cfg_read = msi_addr_hi;
12'h07C: cfg_read = {16'h0000, msi_data};
default: cfg_read = 32'h0000_0000;
endcase
endfunction
// Config write (one DW, full-word only in this demo)
task automatic cfg_write(input logic [11:0] off, input logic [31:0] data);
unique case (off)
12'h004: cfg_command <= data[15:0];
12'h010: cfg_bar0 <= data & 32'hFFFF_F000;
12'h014: cfg_bar1 <= data;
12'h018: cfg_bar2 <= data & 32'hFFFF_0000;
12'h01C: cfg_bar3 <= data;
12'h070: msi_enable <= data[16];
12'h074: msi_addr_lo <= data;
12'h078: msi_addr_hi <= data;
12'h07C: msi_data <= data[15:0];
default: ;
endcase
endtask
// ------------------------------------------------------------------------
// BAR0 register read function
// ------------------------------------------------------------------------
function automatic logic [31:0] reg_read(input logic [11:0] off);
unique case (off)
REG_ID: reg_read = REG_ID_VALUE;
REG_VERSION: reg_read = REG_VER_VALUE;
REG_CTRL: reg_read = {29'h0, reg_dma_dir, reg_irq_enable, 1'b0};
REG_STATUS: reg_read = {29'h0, link_up_in, dma_done, dma_busy};
REG_SCRATCH: reg_read = reg_scratch;
REG_IRQ_STATUS: reg_read = {31'h0, irq_status_dma};
REG_DMA_ADDR_LO: reg_read = reg_dma_addr_lo;
REG_DMA_ADDR_HI: reg_read = reg_dma_addr_hi;
REG_DMA_LEN: reg_read = reg_dma_len;
REG_DMA_OFFSET: reg_read = reg_dma_offset;
default: reg_read = 32'h0000_0000;
endcase
endfunction
// Write one 4-DW beat into BAR0 registers or device RAM.
// The vhost passes absolute PCIe addresses; BAR bases are aligned to
// their window sizes, so addr[15:2] is the RAM word index and
// addr[11:0] is the register offset.
task automatic mem_write_beat(input logic [63:0] addr,
input logic [127:0] data,
input logic is_ram);
if (is_ram) begin
for (int i = 0; i < 4; i++)
dev_ram[(addr[15:2] + i[13:0])] <= data[i*32 +: 32];
end else begin
for (int i = 0; i < 4; i++) begin
unique case (addr[11:0] + i*4)
REG_SCRATCH: reg_scratch <= data[i*32 +: 32];
REG_CTRL: begin
reg_irq_enable <= data[i*32+1];
reg_dma_dir <= data[i*32+2];
if (data[i*32+0]) rw_start_dma <= 1'b1;
end
REG_IRQ_STATUS: if (data[i*32+0]) irq_status_dma <= 1'b0; // W1C
REG_DMA_ADDR_LO: reg_dma_addr_lo <= data[i*32 +: 32];
REG_DMA_ADDR_HI: reg_dma_addr_hi <= data[i*32 +: 32];
REG_DMA_LEN: reg_dma_len <= data[i*32 +: 32];
REG_DMA_OFFSET: reg_dma_offset <= data[i*32 +: 32];
default: ;
endcase
end
end
endtask
// Produce one completion beat for a memory read.
task automatic mem_read_beat(input logic [63:0] addr,
input logic is_ram,
input logic last_flag);
cpl_valid <= 1'b1;
cpl_tag <= cur_tag;
cpl_status <= CPL_SC;
cpl_last <= last_flag;
if (is_ram) begin
for (int i = 0; i < 4; i++)
cpl_data[i*32 +: 32] <= dev_ram[(addr[15:2] + i[13:0])];
end else begin
for (int i = 0; i < 4; i++)
cpl_data[i*32 +: 32] <= reg_read(addr[11:0] + i*4);
end
endtask
// ------------------------------------------------------------------------
// Single sequential process: request FSM + DMA FSM
// ------------------------------------------------------------------------
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
state <= S_IDLE;
cur_addr <= '0;
cur_tag <= '0;
cur_is_ram <= 1'b0;
beats_left <= '0;
cpl_valid <= 1'b0;
cpl_data <= '0;
cpl_tag <= '0;
cpl_last <= 1'b0;
cpl_status <= CPL_SC;
cfg_command <= '0;
cfg_bar0 <= '0;
cfg_bar1 <= '0;
cfg_bar2 <= '0;
cfg_bar3 <= '0;
msi_addr_lo <= '0;
msi_addr_hi <= '0;
msi_data <= '0;
msi_enable <= 1'b0;
reg_scratch <= '0;
reg_dma_addr_lo <= '0;
reg_dma_addr_hi <= '0;
reg_dma_len <= '0;
reg_dma_offset <= '0;
reg_irq_enable <= 1'b0;
reg_dma_dir <= 1'b0;
rw_start_dma <= 1'b0;
irq_status_dma <= 1'b0;
dma_state <= DMA_IDLE;
dma_done <= 1'b0;
dma_bytes_left <= '0;
dma_cur_host <= '0;
dma_cur_dev <= '0;
dma_req_valid <= 1'b0;
dma_req_kind <= TLP_MEM_RD;
dma_req_addr <= '0;
dma_req_len_dw <= '0;
dma_req_data <= '0;
dma_req_last <= 1'b0;
irq_msi <= 1'b0;
end else begin
rw_start_dma <= 1'b0; // strobe default (task may set it below)
irq_msi <= 1'b0; // pulse default (DMA_DONE may set it below)
if (cpl_valid && cpl_ready) cpl_valid <= 1'b0;
// ------------------------------------------------------------
// Request FSM (downstream host requests)
// ------------------------------------------------------------
unique case (state)
S_IDLE: begin
if (req_valid) begin
cur_addr <= req_addr;
cur_tag <= req_tag;
cur_is_ram <= addr_is_ram_now;
unique case (req_kind)
TLP_CFG_WR: cfg_write(req_addr[11:0], req_data[31:0]);
TLP_CFG_RD: begin
cpl_valid <= 1'b1;
cpl_data <= {96'h0, cfg_read(req_addr[11:0])};
cpl_tag <= req_tag;
cpl_last <= 1'b1;
cpl_status <= CPL_SC;
beats_left <= 10'd1;
state <= S_READ_CPL;
end
TLP_MEM_WR: begin
mem_write_beat(req_addr, req_data, addr_is_ram_now);
if (!req_last) state <= S_WRITE;
end
TLP_MEM_RD: begin
beats_left <= (req_len_dw + 10'd3) >> 2;
mem_read_beat(req_addr, addr_is_ram_now,
((req_len_dw + 10'd3) >> 2) <= 10'd1);
state <= S_READ_CPL;
end
endcase
end
end
S_WRITE: begin
if (req_valid) begin
cur_addr <= cur_addr + 64'd16;
mem_write_beat(cur_addr + 64'd16, req_data, cur_is_ram);
if (req_last) state <= S_IDLE;
end
end
S_READ_CPL: begin
if (cpl_valid && cpl_ready) begin
if (beats_left <= 10'd1) begin
state <= S_IDLE;
end else begin
beats_left <= beats_left - 10'd1;
cur_addr <= cur_addr + 64'd16;
mem_read_beat(cur_addr + 64'd16, cur_is_ram,
(beats_left <= 10'd2));
end
end
end
default: state <= S_IDLE;
endcase
// ------------------------------------------------------------
// DMA FSM (runs after the request FSM in this process, so a
// DMA completion set of irq_status wins over a same-cycle W1C)
// ------------------------------------------------------------
unique case (dma_state)
DMA_IDLE: begin
dma_req_valid <= 1'b0;
if (rw_start_dma && (reg_dma_len != 0)) begin
dma_done <= 1'b0;
dma_bytes_left <= reg_dma_len;
dma_cur_host <= reg_dma_addr_lo;
dma_cur_dev <= reg_dma_offset;
dma_state <= reg_dma_dir ? DMA_D2H_REQ : DMA_H2D_REQ;
end
end
DMA_H2D_REQ: begin
dma_req_valid <= 1'b1;
dma_req_kind <= TLP_MEM_RD;
dma_req_addr <= {reg_dma_addr_hi, dma_cur_host};
dma_req_len_dw <= (dma_bytes_left >= 32'd16) ? 10'd4
: dma_bytes_left[11:2];
dma_req_last <= 1'b1;
if (dma_req_valid && dma_req_ready) begin
dma_req_valid <= 1'b0;
dma_state <= DMA_H2D_CPL;
end
end
DMA_H2D_CPL: begin
if (dma_cpl_valid) begin
for (int i = 0; i < 4; i++)
dev_ram[(dma_cur_dev[15:2] + i[13:0])] <= dma_cpl_data[i*32 +: 32];
dma_cur_dev <= dma_cur_dev + 32'd16;
dma_cur_host <= dma_cur_host + 32'd16;
dma_bytes_left <= (dma_bytes_left >= 32'd16) ? dma_bytes_left - 32'd16
: 32'd0;
dma_state <= (dma_bytes_left <= 32'd16) ? DMA_DONE : DMA_H2D_REQ;
end
end
DMA_D2H_REQ: begin
dma_req_valid <= 1'b1;
dma_req_kind <= TLP_MEM_WR;
dma_req_addr <= {reg_dma_addr_hi, dma_cur_host};
dma_req_len_dw <= (dma_bytes_left >= 32'd16) ? 10'd4
: dma_bytes_left[11:2];
dma_req_last <= 1'b1;
for (int i = 0; i < 4; i++)
dma_req_data[i*32 +: 32] <= dev_ram[(dma_cur_dev[15:2] + i[13:0])];
if (dma_req_valid && dma_req_ready) begin
dma_req_valid <= 1'b0;
dma_cur_dev <= dma_cur_dev + 32'd16;
dma_cur_host <= dma_cur_host + 32'd16;
dma_bytes_left <= (dma_bytes_left >= 32'd16) ? dma_bytes_left - 32'd16
: 32'd0;
dma_state <= (dma_bytes_left <= 32'd16) ? DMA_DONE : DMA_D2H_REQ;
end
end
DMA_DONE: begin
dma_done <= 1'b1;
irq_status_dma <= 1'b1;
if (reg_irq_enable && msi_enable) irq_msi <= 1'b1;
dma_state <= DMA_IDLE;
end
default: dma_state <= DMA_IDLE;
endcase
end
end
endmodule : pcie5_ep_core
+41
View File
@@ -0,0 +1,41 @@
// PCIe Gen5 vhost demo - shared definitions
package pcie_pkg;
// TLP request kinds used on the transaction-level interface
// between the vhost transactor and the endpoint core.
typedef enum logic [1:0] {
TLP_CFG_RD = 2'd0,
TLP_CFG_WR = 2'd1,
TLP_MEM_RD = 2'd2,
TLP_MEM_WR = 2'd3
} tlp_kind_e;
// Completion status
localparam logic [2:0] CPL_SC = 3'd0; // successful completion
localparam logic [2:0] CPL_UR = 3'd1; // unsupported request
localparam logic [2:0] CPL_CRS = 3'd2; // configuration retry status
// Demo endpoint identity
localparam logic [15:0] PCIE_VID = 16'h16C3; // Synopsys vendor ID (demo)
localparam logic [15:0] PCIE_DID = 16'h5805; // demo Gen5 endpoint
// PCIe link capability encodings (PCIe Base Spec)
localparam logic [3:0] LINK_SPEED_GEN5 = 4'd6; // 32.0 GT/s
localparam logic [5:0] LINK_WIDTH_X16 = 6'd16;
// BAR0 register offsets
localparam logic [11:0] REG_ID = 12'h000;
localparam logic [11:0] REG_VERSION = 12'h004;
localparam logic [11:0] REG_CTRL = 12'h008;
localparam logic [11:0] REG_STATUS = 12'h00C;
localparam logic [11:0] REG_SCRATCH = 12'h010;
localparam logic [11:0] REG_IRQ_STATUS = 12'h014;
localparam logic [11:0] REG_DMA_ADDR_LO = 12'h018;
localparam logic [11:0] REG_DMA_ADDR_HI = 12'h01C;
localparam logic [11:0] REG_DMA_LEN = 12'h020;
localparam logic [11:0] REG_DMA_OFFSET = 12'h024;
localparam logic [31:0] REG_ID_VALUE = 32'h5035_4531; // "P5E1"
localparam logic [31:0] REG_VER_VALUE = 32'h0001_0000;
endpackage : pcie_pkg