first attempt at cpu monitor

This commit is contained in:
2026-09-29 21:45:29 -04:00
parent 5b5357c2c4
commit 3ae48990b5
2 changed files with 117 additions and 110 deletions
-13
View File
@@ -61,9 +61,6 @@ class cpu_driver #(
$error($stime, " Drv: external write address 0x%0x is not word aligned\n", addr);
end
// TODO: Drive one external write through the clocking block for one request,
// then return the port to idle
cpuVif.cb.addr <= addr;
cpuVif.cb.wdata <= data;
@@ -81,9 +78,6 @@ class cpu_driver #(
$error($stime, " Drv: external read address 0x%0x is not word aligned\n", addr);
end
// TODO: Keep the request stable until rready, capture the returned
// data, and set a timeout for the response
timed_out = 1'b0;
count = 0;
@@ -109,7 +103,6 @@ class cpu_driver #(
// Drive a start request for one CPU run
task start_cpu();
// TODO: Pulse en_cpu
cpuVif.cb.en_cpu <= 1;
@(cpuVif.cb) cpuVif.cb.en_cpu <= 0;
@@ -122,10 +115,6 @@ class cpu_driver #(
limit = (max_cycles == 0) ? run_timeout : max_cycles;
cycles = 0;
// TODO: Check cpu_halted on each cycle and report how many cycles
// ran. Set last_run_completed only after a real halt. On a timeout, increment
// timeouts and leave the flag low so the testbench can recover.
repeat (limit) begin
@(cpuVif.cb) cycles++;
if (cpuVif.cb.cpu_halted) begin
@@ -154,8 +143,6 @@ class cpu_driver #(
// Stop a program through an external halt and wait for memory ownership
// to return to the external port
task halt_cpu_now();
// TODO: Assert an external halt request and wait, with a bound,
// until ownership returns to the external port
cpuVif.cb.halt_cpu <= 1;
@(cpuVif.cb);
+117 -97
View File
@@ -3,124 +3,144 @@
* keeps enough state to match requests with responses, including responses
* that take more than one cycle.
*/
class cpu_monitor #(int DataSize=32, int AddrSize=14);
class cpu_monitor #(
int DataSize = 32,
int AddrSize = 14
);
virtual interface cpu_if #(.DataSize(DataSize), .AddrSize(AddrSize)) cpuVif;
virtual interface cpu_if #(
.DataSize(DataSize),
.AddrSize(AddrSize)
) cpuVif;
mailbox #(cpu_xbar_item #(DataSize, AddrSize)) mon_box;
mailbox #(cpu_xbar_item #(DataSize, AddrSize)) mon_box;
// State for external reads
bit pending_read; // A read is waiting for rready
logic [AddrSize-1:0] pending_read_addr; // Address of that read
// State for external reads
bit pending_read; // A read is waiting for rready
logic [AddrSize-1:0] pending_read_addr; // Address of that read
// State for CPU data-memory requests
bit load_in_flight; // A CPU load is waiting for data
logic [9:0] load_addr; // Address of that load
bit prev_store_valid; // A store was still active last cycle
logic [9:0] prev_store_addr; // Address of that store
logic [DataSize-1:0] prev_store_data; // Data for that store
bit prev_load_valid; // A load was still active last cycle
logic [9:0] prev_load_addr; // Address of that load
// State for CPU data-memory requests
bit load_in_flight; // A CPU load is waiting for data
logic [9:0] load_addr; // Address of that load
bit prev_store_valid; // A store was still active last cycle
logic [9:0] prev_store_addr; // Address of that store
logic [DataSize-1:0] prev_store_data; // Data for that store
bit prev_load_valid; // A load was still active last cycle
logic [9:0] prev_load_addr; // Address of that load
// Detecting state changes
bit prev_cpu_active; // Previous CPU memory-ownership state
bit prev_cpu_halted; // Previous CPU halt state
bit prev_reset; // Previous reset state
int errors; // Number of interface errors found
// Detecting state changes
bit prev_cpu_active; // Previous CPU memory-ownership state
bit prev_cpu_halted; // Previous CPU halt state
bit prev_reset; // Previous reset state
int errors; // Number of interface errors found
bit log_transactions; // Optional transaction logging flag
bit log_transactions; // Optional transaction logging flag
// Constructor
function new(virtual interface cpu_if #(DataSize, AddrSize) cpuVif);
this.cpuVif = cpuVif;
this.mon_box = new();
this.pending_read = 1'b0;
this.load_in_flight = 1'b0;
this.prev_store_valid = 1'b0;
this.prev_load_valid = 1'b0;
this.prev_cpu_active = 1'b0;
this.prev_cpu_halted = 1'b0;
this.prev_reset = 1'b0;
this.log_transactions = 1'b1;
this.errors = 0;
endfunction: new
// Constructor
function new(virtual interface cpu_if #(DataSize, AddrSize) cpuVif);
this.cpuVif = cpuVif;
this.mon_box = new();
this.pending_read = 1'b0;
this.load_in_flight = 1'b0;
this.prev_store_valid = 1'b0;
this.prev_load_valid = 1'b0;
this.prev_cpu_active = 1'b0;
this.prev_cpu_halted = 1'b0;
this.prev_reset = 1'b0;
this.log_transactions = 1'b1;
this.errors = 0;
endfunction : new
// Create a transaction for an observed event and send it to the scoreboard
task send(input trans_kind_e kind,
input logic [AddrSize-1:0] addr, input logic [DataSize-1:0] data,
input logic is_write=1'b0);
// Create a transaction for an observed event and send it to the scoreboard
task send(input trans_kind_e kind, input logic [AddrSize-1:0] addr,
input logic [DataSize-1:0] data, input logic is_write = 1'b0);
// TODO: Create and send an xbar transaction through the mailbox
// TODO: Create and send an xbar transaction through the mailbox
$fatal(1, "TODO: Implement cpu_monitor.send");
endtask: send
cpu_xbar_item #(DataSize, AddrSize) txn = new();
txn.kind = kind;
txn.addr = addr;
txn.is_write = is_write;
// Match each external read request with its response
task observe_external_read();
// TODO: Match each external read request with its response. Save
// the address when the request starts, and send one XBAR_READ when rready
// shows that rdata is valid. How should you handle register file vs SRAM
// reads?
mon_box.put(txn);
endtask : send
$fatal(1, "Implement cpu_monitor.observe_external_read");
endtask: observe_external_read
// Match each external read request with its response
task observe_external_read();
// TODO: Match each external read request with its response. Save
// the address when the request starts, and send one XBAR_READ when rready
// shows that rdata is valid. How should you handle register file vs SRAM
// reads?
// Report each CPU data-memory request once
task observe_cpu_memory();
// TODO: Report each CPU data-memory request once. Send a CPU_LOAD
// when its read data returns and a CPU_STORE when its write completes,
// even if the request stays active during an SRAM wait. Clear saved state
// after the request ends.
if (!pending_read) begin
pending_read = 1'b1;
pending_read_addr = cpuVif.cb.addr;
end
$fatal(1, "Implement cpu_monitor.observe_cpu_memory");
endtask: observe_cpu_memory
if (cpuVif.cb.rready) begin
send(XBAR_READ, pending_read_addr, cpuVif.cb.rdata);
pending_read = 1'b0;
end
endtask : observe_external_read
// Monitor the DUT once per clock and pass new events to the scoreboard
task run();
forever begin
@(cpuVif.cb);
// Report each CPU data-memory request once
task observe_cpu_memory();
// TODO: Report each CPU data-memory request once. Send a CPU_LOAD
// when its read data returns and a CPU_STORE when its write completes,
// even if the request stays active during an SRAM wait. Clear saved state
// after the request ends.
if (cpuVif.cb.reset && !prev_reset) begin
send(DUT_RESET, '0, '0);
pending_read = 1'b0;
load_in_flight = 1'b0;
prev_store_valid = 1'b0;
prev_load_valid = 1'b0;
$display($stime, " Mon: Reset asserted\n");
end
prev_reset = cpuVif.cb.reset;
if (cpuVif.cb.cpu_active && !prev_cpu_active) begin
send(CPU_START, '0, '0);
$display($stime, " Mon: CPU started executing\n");
end
$fatal(1, "Implement cpu_monitor.observe_cpu_memory");
endtask : observe_cpu_memory
if (cpuVif.cb.cpu_halted && !prev_cpu_halted) begin
send(CPU_HALT, '0, '0);
$display($stime, " Mon: cpu_halted_o asserted\n");
end
// Monitor the DUT once per clock and pass new events to the scoreboard
task run();
forever begin
@(cpuVif.cb);
if (cpuVif.cb.w_en && !cpuVif.cb.cpu_active) begin
if (log_transactions) begin
$display($stime, " Mon: External write Addr: 0x%04x, Data: 0x%08x\n",
cpuVif.cb.addr, cpuVif.cb.wdata);
end
send(XBAR_WRITE, cpuVif.cb.addr, cpuVif.cb.wdata, 1'b1);
end
if (cpuVif.cb.reset && !prev_reset) begin
send(DUT_RESET, '0, '0);
pending_read = 1'b0;
load_in_flight = 1'b0;
prev_store_valid = 1'b0;
prev_load_valid = 1'b0;
$display($stime, " Mon: Reset asserted\n");
end
prev_reset = cpuVif.cb.reset;
if ((cpuVif.cb.r_en && !cpuVif.cb.cpu_active) || pending_read) begin
observe_external_read();
end
if (cpuVif.cb.cpu_active && !prev_cpu_active) begin
send(CPU_START, '0, '0);
$display($stime, " Mon: CPU started executing\n");
end
if (load_in_flight || (cpuVif.cb.cpu_active && cpuVif.cb.dsram_en) ||
prev_store_valid || prev_load_valid) begin
observe_cpu_memory();
end
if (cpuVif.cb.cpu_halted && !prev_cpu_halted) begin
send(CPU_HALT, '0, '0);
$display($stime, " Mon: cpu_halted_o asserted\n");
end
prev_cpu_active = cpuVif.cb.cpu_active;
prev_cpu_halted = cpuVif.cb.cpu_halted;
if (cpuVif.cb.w_en && !cpuVif.cb.cpu_active) begin
if (log_transactions) begin
$display($stime, " Mon: External write Addr: 0x%04x, Data: 0x%08x\n", cpuVif.cb.addr,
cpuVif.cb.wdata);
end
endtask: run
endclass: cpu_monitor
send(XBAR_WRITE, cpuVif.cb.addr, cpuVif.cb.wdata, 1'b1);
end
if ((cpuVif.cb.r_en && !cpuVif.cb.cpu_active) || pending_read) begin
observe_external_read();
end
if (load_in_flight || (cpuVif.cb.cpu_active && cpuVif.cb.dsram_en) ||
prev_store_valid || prev_load_valid) begin
observe_cpu_memory();
end
prev_cpu_active = cpuVif.cb.cpu_active;
prev_cpu_halted = cpuVif.cb.cpu_halted;
end
endtask : run
endclass : cpu_monitor