From 3ae48990b5adec48e4f856b46df062da51fb256f Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Tue, 29 Sep 2026 21:45:29 -0400 Subject: [PATCH] first attempt at cpu monitor --- src/verilog/tb/cpu_driver.svh | 13 -- src/verilog/tb/cpu_monitor.svh | 214 ++++++++++++++++++--------------- 2 files changed, 117 insertions(+), 110 deletions(-) diff --git a/src/verilog/tb/cpu_driver.svh b/src/verilog/tb/cpu_driver.svh index d9de6a4..c036b7d 100644 --- a/src/verilog/tb/cpu_driver.svh +++ b/src/verilog/tb/cpu_driver.svh @@ -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); diff --git a/src/verilog/tb/cpu_monitor.svh b/src/verilog/tb/cpu_monitor.svh index bd7c6d4..7670e67 100644 --- a/src/verilog/tb/cpu_monitor.svh +++ b/src/verilog/tb/cpu_monitor.svh @@ -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 \ No newline at end of file + 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 +