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); $error($stime, " Drv: external write address 0x%0x is not word aligned\n", addr);
end 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.addr <= addr;
cpuVif.cb.wdata <= data; 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); $error($stime, " Drv: external read address 0x%0x is not word aligned\n", addr);
end end
// TODO: Keep the request stable until rready, capture the returned
// data, and set a timeout for the response
timed_out = 1'b0; timed_out = 1'b0;
count = 0; count = 0;
@@ -109,7 +103,6 @@ class cpu_driver #(
// Drive a start request for one CPU run // Drive a start request for one CPU run
task start_cpu(); task start_cpu();
// TODO: Pulse en_cpu
cpuVif.cb.en_cpu <= 1; cpuVif.cb.en_cpu <= 1;
@(cpuVif.cb) cpuVif.cb.en_cpu <= 0; @(cpuVif.cb) cpuVif.cb.en_cpu <= 0;
@@ -122,10 +115,6 @@ class cpu_driver #(
limit = (max_cycles == 0) ? run_timeout : max_cycles; limit = (max_cycles == 0) ? run_timeout : max_cycles;
cycles = 0; 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 repeat (limit) begin
@(cpuVif.cb) cycles++; @(cpuVif.cb) cycles++;
if (cpuVif.cb.cpu_halted) begin 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 // Stop a program through an external halt and wait for memory ownership
// to return to the external port // to return to the external port
task halt_cpu_now(); 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.halt_cpu <= 1;
@(cpuVif.cb); @(cpuVif.cb);
+117 -97
View File
@@ -3,124 +3,144 @@
* keeps enough state to match requests with responses, including responses * keeps enough state to match requests with responses, including responses
* that take more than one cycle. * 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 // State for external reads
bit pending_read; // A read is waiting for rready bit pending_read; // A read is waiting for rready
logic [AddrSize-1:0] pending_read_addr; // Address of that read logic [AddrSize-1:0] pending_read_addr; // Address of that read
// State for CPU data-memory requests // State for CPU data-memory requests
bit load_in_flight; // A CPU load is waiting for data bit load_in_flight; // A CPU load is waiting for data
logic [9:0] load_addr; // Address of that load logic [9:0] load_addr; // Address of that load
bit prev_store_valid; // A store was still active last cycle bit prev_store_valid; // A store was still active last cycle
logic [9:0] prev_store_addr; // Address of that store logic [9:0] prev_store_addr; // Address of that store
logic [DataSize-1:0] prev_store_data; // Data for that store logic [DataSize-1:0] prev_store_data; // Data for that store
bit prev_load_valid; // A load was still active last cycle bit prev_load_valid; // A load was still active last cycle
logic [9:0] prev_load_addr; // Address of that load logic [9:0] prev_load_addr; // Address of that load
// Detecting state changes // Detecting state changes
bit prev_cpu_active; // Previous CPU memory-ownership state bit prev_cpu_active; // Previous CPU memory-ownership state
bit prev_cpu_halted; // Previous CPU halt state bit prev_cpu_halted; // Previous CPU halt state
bit prev_reset; // Previous reset state bit prev_reset; // Previous reset state
int errors; // Number of interface errors found int errors; // Number of interface errors found
bit log_transactions; // Optional transaction logging flag bit log_transactions; // Optional transaction logging flag
// Constructor // Constructor
function new(virtual interface cpu_if #(DataSize, AddrSize) cpuVif); function new(virtual interface cpu_if #(DataSize, AddrSize) cpuVif);
this.cpuVif = cpuVif; this.cpuVif = cpuVif;
this.mon_box = new(); this.mon_box = new();
this.pending_read = 1'b0; this.pending_read = 1'b0;
this.load_in_flight = 1'b0; this.load_in_flight = 1'b0;
this.prev_store_valid = 1'b0; this.prev_store_valid = 1'b0;
this.prev_load_valid = 1'b0; this.prev_load_valid = 1'b0;
this.prev_cpu_active = 1'b0; this.prev_cpu_active = 1'b0;
this.prev_cpu_halted = 1'b0; this.prev_cpu_halted = 1'b0;
this.prev_reset = 1'b0; this.prev_reset = 1'b0;
this.log_transactions = 1'b1; this.log_transactions = 1'b1;
this.errors = 0; this.errors = 0;
endfunction: new endfunction : new
// Create a transaction for an observed event and send it to the scoreboard // Create a transaction for an observed event and send it to the scoreboard
task send(input trans_kind_e kind, task send(input trans_kind_e kind, input logic [AddrSize-1:0] addr,
input logic [AddrSize-1:0] addr, input logic [DataSize-1:0] data, input logic [DataSize-1:0] data, input logic is_write = 1'b0);
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"); cpu_xbar_item #(DataSize, AddrSize) txn = new();
endtask: send txn.kind = kind;
txn.addr = addr;
txn.is_write = is_write;
// Match each external read request with its response mon_box.put(txn);
task observe_external_read(); endtask : send
// 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?
$fatal(1, "Implement cpu_monitor.observe_external_read"); // Match each external read request with its response
endtask: observe_external_read 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 if (!pending_read) begin
task observe_cpu_memory(); pending_read = 1'b1;
// TODO: Report each CPU data-memory request once. Send a CPU_LOAD pending_read_addr = cpuVif.cb.addr;
// when its read data returns and a CPU_STORE when its write completes, end
// even if the request stays active during an SRAM wait. Clear saved state
// after the request ends.
$fatal(1, "Implement cpu_monitor.observe_cpu_memory"); if (cpuVif.cb.rready) begin
endtask: observe_cpu_memory 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 // Report each CPU data-memory request once
task run(); task observe_cpu_memory();
forever begin // TODO: Report each CPU data-memory request once. Send a CPU_LOAD
@(cpuVif.cb); // 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 $fatal(1, "Implement cpu_monitor.observe_cpu_memory");
send(CPU_START, '0, '0); endtask : observe_cpu_memory
$display($stime, " Mon: CPU started executing\n");
end
if (cpuVif.cb.cpu_halted && !prev_cpu_halted) begin // Monitor the DUT once per clock and pass new events to the scoreboard
send(CPU_HALT, '0, '0); task run();
$display($stime, " Mon: cpu_halted_o asserted\n"); forever begin
end @(cpuVif.cb);
if (cpuVif.cb.w_en && !cpuVif.cb.cpu_active) begin if (cpuVif.cb.reset && !prev_reset) begin
if (log_transactions) begin send(DUT_RESET, '0, '0);
$display($stime, " Mon: External write Addr: 0x%04x, Data: 0x%08x\n", pending_read = 1'b0;
cpuVif.cb.addr, cpuVif.cb.wdata); load_in_flight = 1'b0;
end prev_store_valid = 1'b0;
prev_load_valid = 1'b0;
send(XBAR_WRITE, cpuVif.cb.addr, cpuVif.cb.wdata, 1'b1); $display($stime, " Mon: Reset asserted\n");
end end
prev_reset = cpuVif.cb.reset;
if ((cpuVif.cb.r_en && !cpuVif.cb.cpu_active) || pending_read) begin if (cpuVif.cb.cpu_active && !prev_cpu_active) begin
observe_external_read(); send(CPU_START, '0, '0);
end $display($stime, " Mon: CPU started executing\n");
end
if (load_in_flight || (cpuVif.cb.cpu_active && cpuVif.cb.dsram_en) || if (cpuVif.cb.cpu_halted && !prev_cpu_halted) begin
prev_store_valid || prev_load_valid) begin send(CPU_HALT, '0, '0);
observe_cpu_memory(); $display($stime, " Mon: cpu_halted_o asserted\n");
end end
prev_cpu_active = cpuVif.cb.cpu_active; if (cpuVif.cb.w_en && !cpuVif.cb.cpu_active) begin
prev_cpu_halted = cpuVif.cb.cpu_halted; if (log_transactions) begin
$display($stime, " Mon: External write Addr: 0x%04x, Data: 0x%08x\n", cpuVif.cb.addr,
cpuVif.cb.wdata);
end 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