From 4fb940740b46eaa960a7d01a252b3fcf17d7cf63 Mon Sep 17 00:00:00 2001 From: Simon Southwell Date: Mon, 22 Apr 2024 16:28:18 +0100 Subject: [PATCH] Made Verilog and SystemVerilog VProc HDL common source --- f_VProc.sv | 258 +-------------------------------------------------- f_VProc.v | 50 +++++----- vprocdefs.vh | 30 +++++- 3 files changed, 58 insertions(+), 280 deletions(-) diff --git a/f_VProc.sv b/f_VProc.sv index 7b5d332..f651b98 100644 --- a/f_VProc.sv +++ b/f_VProc.sv @@ -22,259 +22,9 @@ // // ==================================================================== -`include "vprocdefs.vh" -`include "vprocdpi.vh" +// The SystemVerilog VProc HDL is the same as the Verilog, +// with VPROC_SV defined to select DPI-C over VPI -// ============================================================ -// VProc module -// ============================================================ +`define VPROC_SV -module VProc -#(parameter INT_WIDTH = 3, - NODE_WIDTH = 4, - BURST_ADDR_INCR = 1 -) -( - // Clock - input Clk, - - // Bus interface - output reg [31:0] Addr, - output reg WE, - output reg RD, - output reg [31:0] DataOut, - input [31:0] DataIn, - input WRAck, - input RDAck, - - // Interrupt - input [INT_WIDTH-1:0] Interrupt, - - // Delta cycle control - output reg Update, - input UpdateResponse, - -`ifdef VPROC_BURST_IF - // Burst count - output reg [11:0] Burst, - output reg BurstFirst, - output reg BurstLast, -`endif - - // Node number - input [NODE_WIDTH-1:0] Node -); - -// ------------------------------------------------------------ -// Register definitions -// ------------------------------------------------------------ - -// VSched/VAccess outputs -integer VPDataOut; -integer VPAddr; -integer VPRW; -integer VPTicks; - -// Sampled VProc inputs -integer DataInSamp; -integer IntSamp; -integer IntSampLast; -integer NodeI; -reg RdAckSamp; -reg WRAckSamp; - -// Internal initialised flag (set after VInit called) -reg Initialised; - -// Internal state -integer TickCount; -integer BlkCount; -integer AccIdx; - -`ifndef VPROC_BURST_IF - -// When no burst interface define some local dummy registers to -// replace the missing ports -reg [11:0] Burst; -reg BurstFirst; -reg BurstLast; - -task vdummy (input integer a, input integer b, input integer c, output integer d); -begin -end -endtask - -// vaccess is not defined when no burst interface -`define vaccess vdummy - -`else - -// When a burst interface defined, use $vaccess -`define vaccess VAccess - -`endif - -// ------------------------------------------------------------ -// Initial process -// ------------------------------------------------------------ - -initial -begin - TickCount = 1; - Initialised = 0; - WE = 0; - RD = 0; - Update = 0; - BlkCount = 0; - IntSampLast = 0; - - // Don't remove delay! Needed to allow Node to be assigned - // before the call to VInit - `MINDELAY - VInit(Node); - Initialised = 1; -end - - -// ------------------------------------------------------------ -/// Main scheduler process -// ------------------------------------------------------------ - -always @(posedge Clk) -begin - // Cleanly sample the inputs and make them integers - RdAckSamp = RDAck; - WRAckSamp = WRAck; - IntSamp = {1'b0, Interrupt}; - NodeI = Node; - VPTicks = `DELTACYCLE; - - // Wait until the VProc software is initialised for this node (VInit called) - // before starting accesses - if (Initialised == 1'b1) - begin - // If an interrupt active, call VSched with interrupt value - if (IntSamp > 0) - begin - VSched(NodeI, IntSamp, DataInSamp, VPDataOut, VPAddr, VPRW, VPTicks); - - // If interrupt routine returns non-zero tick, then override - // current tick value. Otherwise, leave at present value. - if (VPTicks > 0) - begin - TickCount = VPTicks; - end - end - - // If vector IRQ enabled, call VIirq when interrupt value changes, passing in - // new value - if (IntSamp != IntSampLast) - begin - VIrq(NodeI, IntSamp); - IntSampLast <= IntSamp; - end - - // If tick, write or a read has completed (or in last cycle)... - if ((RD === 1'b0 && WE === 1'b0 && TickCount === 0) || - (RD === 1'b1 && RdAckSamp === 1'b1) || - (WE === 1'b1 && WRAckSamp === 1'b1)) - begin - BurstFirst <= 1'b0; - BurstLast <= 1'b0; - - // Loop accessing new commands until VPTicks is not a delta cycle update - while (VPTicks < 0) - begin - // Clear any interrupt (already dealt with) - IntSamp = 0; - - // Sample the data in port - DataInSamp = DataIn; - - if (BlkCount <= 1) - begin - // If this is the last transfer in a burst, call VAccess with - // the last data input sample. - if (BlkCount == 1) - begin - AccIdx = AccIdx + 1; - BlkCount = 0; - `vaccess(NodeI, AccIdx, DataInSamp, VPDataOut); - end - - // Get new access command - VSched(NodeI, IntSamp, DataInSamp, VPDataOut, VPAddr, VPRW, VPTicks); - - // Update the outputs - Burst <= VPRW[`BLKBITS]; - WE <= VPRW[`WEBIT]; - RD <= VPRW[`RDBIT]; - Addr <= VPAddr; - - // If new BlkCount is non-zero, setup burst transfer - if (VPRW[`BLKBITS] !== 0) - begin - // Flag burst as first in block - BurstFirst <= 1'b1; - - // Initialise the burst block counter with count bits - BlkCount = VPRW[`BLKBITS]; - - // On writes, override VPDataOut to get from burst access task VAccess at index 0 - if (VPRW[`WEBIT]) - begin - AccIdx = 0; - `vaccess(NodeI, AccIdx, `DONTCARE, VPDataOut); - end - else - begin - // For reads, intialise index to -1, as it's pre-incremented at next VAccess call - AccIdx = -1; - end - end - - // Update DataOut port - DataOut <= VPDataOut; - end - // If a block access is valid (BlkCount is non-zero), get the next data out/send back latest sample - else - begin - AccIdx = AccIdx + 1; - `vaccess(NodeI, AccIdx, DataInSamp, VPDataOut); - BlkCount = BlkCount - 1; - - if (BlkCount == 1) - begin - BurstLast <= 1'b1; - end - - // When bursting, reassert non-delta VPTicks value to break out of loop. - VPTicks = 0; - - // Update address and data outputs - DataOut <= VPDataOut; - Addr <= Addr + BURST_ADDR_INCR; - end - - // Update current tick value with returned number (if not negative) - if (VPTicks > 0) - begin - TickCount = VPTicks - 1; - end - - // Flag to update externally and wait for response. - // The `MINDELAY ensures it's not updated until other outputs - // are updated. - Update <= `MINDELAY ~Update; - @(UpdateResponse); - end - end - else - begin - // Count down to zero and stop - TickCount = (TickCount > 0) ? TickCount - 1 : 0; - end - end -end - -endmodule +`include "f_VProc.v" \ No newline at end of file diff --git a/f_VProc.v b/f_VProc.v index afdeed0..09932a3 100644 --- a/f_VProc.v +++ b/f_VProc.v @@ -1,9 +1,9 @@ // ==================================================================== // -// Verilog side Virtual Processor, for running host +// SystemVerilog side Virtual Processor, for running host // programs as control in simulation. // -// Copyright (c) 2004-2024 Simon Southwell. +// Copyright (c) 2024 Simon Southwell. // // This file is part of VProc. // @@ -24,6 +24,10 @@ `include "vprocdefs.vh" +`ifdef VPROC_SV +`include "vprocdpi.vh" +`endif + // ============================================================ // VProc module // ============================================================ @@ -68,7 +72,7 @@ module VProc // Register definitions // ------------------------------------------------------------ -// $vsched/$vaccess outputs +// VSched/VAccess outputs integer VPDataOut; integer VPAddr; integer VPRW; @@ -82,7 +86,7 @@ integer NodeI; reg RdAckSamp; reg WRAckSamp; -// Internal initialised flag (set after $vinit called) +// Internal initialised flag (set after VInit called) reg Initialised; // Internal state @@ -103,13 +107,13 @@ begin end endtask -// $vaccess is not defined when no burst interface +// vaccess is not defined when no burst interface `define vaccess vdummy `else -// When a burst interface defined, use $vaccess -`define vaccess $vaccess +// When a burst interface defined, use $vaccess/VAccess (for VPI or DPI-C) +`define vaccess `VAccess `endif @@ -128,9 +132,9 @@ begin IntSampLast = 0; // Don't remove delay! Needed to allow Node to be assigned - // before the call to $vinit - #0 - $vinit(Node); + // before the call to VInit + `MINDELAY + `VInit(Node); Initialised = 1; end @@ -148,14 +152,14 @@ begin NodeI = Node; VPTicks = `DELTACYCLE; - // Wait until the VProc software is initialised for this node ($vinit called) + // Wait until the VProc software is initialised for this node (VInit called) // before starting accesses if (Initialised == 1'b1) begin - // If an interrupt active, call $vsched with interrupt value + // If an interrupt active, call VSched with interrupt value if (IntSamp > 0) begin - $vsched(NodeI, IntSamp, DataInSamp, VPDataOut, VPAddr, VPRW, VPTicks); + `VSched(NodeI, IntSamp, DataInSamp, VPDataOut, VPAddr, VPRW, VPTicks); // If interrupt routine returns non-zero tick, then override // current tick value. Otherwise, leave at present value. @@ -165,11 +169,11 @@ begin end end - // If vector IRQ enabled, call $virq when interrupt value changes, passing in + // If vector IRQ enabled, call VIirq when interrupt value changes, passing in // new value if (IntSamp != IntSampLast) begin - $virq(NodeI, IntSamp); + `VIrq(NodeI, IntSamp); IntSampLast <= IntSamp; end @@ -192,7 +196,7 @@ begin if (BlkCount <= 1) begin - // If this is the last transfer in a burst, call $vaccess with + // If this is the last transfer in a burst, call VAccess with // the last data input sample. if (BlkCount == 1) begin @@ -202,7 +206,7 @@ begin end // Get new access command - $vsched(NodeI, IntSamp, DataInSamp, VPDataOut, VPAddr, VPRW, VPTicks); + `VSched(NodeI, IntSamp, DataInSamp, VPDataOut, VPAddr, VPRW, VPTicks); // Update the outputs Burst <= VPRW[`BLKBITS]; @@ -215,11 +219,11 @@ begin begin // Flag burst as first in block BurstFirst <= 1'b1; - + // Initialise the burst block counter with count bits BlkCount = VPRW[`BLKBITS]; - // On writes, override VPDataOut to get from burst access task $vaccess at index 0 + // On writes, override VPDataOut to get from burst access task VAccess at index 0 if (VPRW[`WEBIT]) begin AccIdx = 0; @@ -227,7 +231,7 @@ begin end else begin - // For reads, intialise index to -1, as it's pre-incremented at next $vaccess call + // For reads, intialise index to -1, as it's pre-incremented at next VAccess call AccIdx = -1; end end @@ -249,7 +253,7 @@ begin // When bursting, reassert non-delta VPTicks value to break out of loop. VPTicks = 0; - + // Update address and data outputs DataOut <= VPDataOut; Addr <= Addr + BURST_ADDR_INCR; @@ -262,9 +266,9 @@ begin end // Flag to update externally and wait for response. - // The #0 ensures it's not updated until other outputs + // The `MINDELAY ensures it's not updated until other outputs // are updated. - Update <= #0 ~Update; + Update <= `MINDELAY ~Update; @(UpdateResponse); end end diff --git a/vprocdefs.vh b/vprocdefs.vh index bee26b7..94c4e94 100644 --- a/vprocdefs.vh +++ b/vprocdefs.vh @@ -33,8 +33,32 @@ `define DELTACYCLE -1 `define DONTCARE 0 -`ifndef VERILATOR -`define MINDELAY #0 -`else +`ifdef VERILATOR + `define MINDELAY /**/ + +`else + +`define MINDELAY #0 + +`endif + +// If SystemVerilog map PLI definitions to DPI-C tasks +`ifdef VPROC_SV + +`define VAccess VAccess +`define VInit VInit +`define VSched VSched +`define VIrq VIrq +`define VProcUser VProcUser + +// If Verilog map PLI deinitions to VPI system tasks +`else + +`define VAccess $vaccess +`define VInit $vinit +`define VSched $vsched +`define VIrq $virq +`define VProcUser $vprocuser + `endif