first attempt at driver

This commit is contained in:
2026-09-29 21:17:52 -04:00
parent d4d1c5edbf
commit 21c1f2837a
+152 -93
View File
@@ -7,122 +7,181 @@
* the CPU and the external port, and acts as a crossbar (xbar) to route external * the CPU and the external port, and acts as a crossbar (xbar) to route external
* access to ISRAM, DSRAM, and registers. * access to ISRAM, DSRAM, and registers.
*/ */
class cpu_driver #(int DataSize=32, int AddrSize=14); class cpu_driver #(
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)) xbar_box; mailbox #(cpu_xbar_item #(DataSize, AddrSize)) xbar_box;
// Maximum cycles to wait for an external read to finish // Maximum cycles to wait for an external read to finish
int xbar_timeout = 32; int xbar_timeout = 32;
// Maximum cycles to wait for the CPU to halt // Maximum cycles to wait for the CPU to halt
int run_timeout = 20000; int run_timeout = 20000;
int timeouts; // Counts timeout failures int timeouts; // Counts timeout failures
bit last_run_completed; // High when the CPU reports a clean halt bit last_run_completed; // High when the CPU reports a clean halt
// Constructor // Constructor
function new(virtual interface cpu_if #(DataSize, AddrSize) cpuVif, function new(virtual interface cpu_if #(DataSize, AddrSize) cpuVif,
mailbox #(cpu_xbar_item #(DataSize, AddrSize)) xbar_box); mailbox#(cpu_xbar_item#(DataSize, AddrSize)) xbar_box);
this.cpuVif = cpuVif; this.cpuVif = cpuVif;
this.xbar_box = xbar_box; this.xbar_box = xbar_box;
this.timeouts = 0; this.timeouts = 0;
this.last_run_completed = 1'b0; this.last_run_completed = 1'b0;
endfunction: new endfunction : new
// Put the DUT into a known reset state // Put the DUT into a known reset state
task reset_task(); task reset_task();
// TODO: Drive all requests inactive, hold reset for several clock // TODO: Drive all requests inactive, hold reset for several clock
// cycles, then release it and give the interface time to settle // cycles, then release it and give the interface time to settle
$fatal(1, "implement cpu_driver.reset_task"); cpuVif.cb.en_cpu <= 0;
endtask: reset_task cpuVif.cb.halt_cpu <= 0;
cpuVif.cb.addr <= 0;
cpuVif.cb.wdata <= 0;
cpuVif.cb.w_en <= 0;
cpuVif.cb.r_en <= 0;
// Send one write through the DUT's external memory port cpuVif.cb.reset <= 1;
task xbar_write(input logic [AddrSize-1:0] addr, input logic [DataSize-1:0] data); repeat (4) @(cpuVif.cb);
if (addr[1:0] != 2'b00) begin cpuVif.cb.reset <= 0;
$error($stime, " Drv: external write address 0x%0x is not word aligned\n", addr); repeat (2) @(cpuVif.cb);
end endtask : reset_task
// TODO: Drive one external write through the clocking block for one request, // Send one write through the DUT's external memory port
// then return the port to idle task xbar_write(input logic [AddrSize-1:0] addr, input logic [DataSize-1:0] data);
if (addr[1:0] != 2'b00) begin
$error($stime, " Drv: external write address 0x%0x is not word aligned\n", addr);
end
$fatal(1, "implement cpu_driver.xbar_write"); // TODO: Drive one external write through the clocking block for one request,
endtask: xbar_write // then return the port to idle
// Send one read through the external memory port and wait for its data cpuVif.cb.addr <= addr;
task xbar_read(input logic [AddrSize-1:0] addr); cpuVif.cb.data <= data;
bit timed_out;
int count;
if (addr[1:0] != 2'b00) begin cpuVif.cb.w_en <= 1;
$error($stime, " Drv: external read address 0x%0x is not word aligned\n", addr); @(cpuVif.cb);
end cpuVif.cb.w_en <= 0;
endtask : xbar_write
// TODO: Keep the request stable until rready, capture the returned // Send one read through the external memory port and wait for its data
// data, and set a timeout for the response task xbar_read(input logic [AddrSize-1:0] addr);
bit timed_out;
int count;
if (addr[1:0] != 2'b00) begin
$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;
cpuVif.cb.addr <= addr;
cpuVif.cb.r_en <= 1;
timed_out = 1'b1;
repeat (xbar_timeout) begin
@(cpuVif.cb);
if (cpuVif.cb.rready) begin
timed_out = 1'b0; timed_out = 1'b0;
count = 0; break;
end
end
$fatal(1, "implement cpu_driver.xbar_read"); if (timed_out) begin
endtask: xbar_read timeouts++;
$error($stime, " Drv: read 0x%0x timed out\n", addr);
end
// Drive a start request for one CPU run cpuVif.cb.r_en <= 0;
task start_cpu(); endtask : xbar_read
// TODO: Pulse en_cpu
$fatal(1, "implement cpu_driver.start_cpu"); // Drive a start request for one CPU run
endtask: start_cpu task start_cpu();
// TODO: Pulse en_cpu
// Wait for the CPU to halt, or fail after the allowed number of cycles cpuVif.cb.en_cpu <= 1;
task wait_for_halt(output int cycles, input int max_cycles=0); @(cpuVif.cb) cpuVif.cb.en_cpu <= 0;
int limit; endtask : start_cpu
limit = (max_cycles == 0) ? run_timeout : max_cycles; // Wait for the CPU to halt, or fail after the allowed number of cycles
cycles = 0; task wait_for_halt(output int cycles, input int max_cycles = 0);
int limit;
// TODO: Check cpu_halted on each cycle and report how many cycles limit = (max_cycles == 0) ? run_timeout : max_cycles;
// ran. Set last_run_completed only after a real halt. On a timeout, increment cycles = 0;
// timeouts and leave the flag low so the testbench can recover.
last_run_completed = 1'b0; // 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.
$fatal(1, "implement cpu_driver.wait_for_halt"); repeat (limit) begin
endtask: wait_for_halt @(cpuVif.cb) cycles++;
if (cpuVif.cb.cpu_halted) begin
// Start CPU and wait for it to halt @(cpuVif.cb)
task run_program(input int max_cycles=0); // real halt goes high then low after one pulse
int cycles; if (!cpuVif.cb.cpu_halted) begin
last_run_completed = 1'b1;
last_run_completed = 1'b0; return;
start_cpu();
wait_for_halt(cycles, max_cycles);
endtask: run_program
// 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
$fatal(1, "TODO: implement cpu_driver.halt_cpu_now");
endtask: halt_cpu_now
// Main driver loop. Keeps taking transactions from the mailbox and
// driving them through the DUT
task run();
cpu_xbar_item #(DataSize, AddrSize) trans;
forever begin
xbar_box.get(trans);
if (trans.is_write) begin
xbar_write(trans.addr, trans.data);
end else begin
xbar_read(trans.addr);
end
end end
endtask: run end
end
endclass: cpu_driver timeouts++;
last_run_completed = 1'b0;
endtask : wait_for_halt
// Start CPU and wait for it to halt
task run_program(input int max_cycles = 0);
int cycles;
last_run_completed = 1'b0;
start_cpu();
wait_for_halt(cycles, max_cycles);
endtask : run_program
// 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);
cpuVif.cb.halt_cpu <= 0;
repeat (xbar_timeout) begin
@(cpuVif.cb);
if (!cpuVif.cb.cpu_active) return;
end
timeouts++;
endtask : halt_cpu_now
// Main driver loop. Keeps taking transactions from the mailbox and
// driving them through the DUT
task run();
cpu_xbar_item #(DataSize, AddrSize) trans;
forever begin
xbar_box.get(trans);
if (trans.is_write) begin
xbar_write(trans.addr, trans.data);
end else begin
xbar_read(trans.addr);
end
end
endtask : run
endclass : cpu_driver