Refine tb main.

This commit is contained in:
Colin 2025-03-29 16:22:05 +08:00
parent aaad0d85a5
commit 393499537d
1 changed files with 1 additions and 345 deletions

View File

@ -44,231 +44,6 @@ enum {
IO_MTIMECMP1H = 0x114
};
// struct mem_io_state {
// uint64_t mtime;
// uint64_t mtimecmp[2];
// bool exit_req;
// uint32_t exit_code;
// uint8_t *mem;
// bool monitor_enabled;
// bool reservation_valid[2];
// uint32_t reservation_addr[2];
// mem_io_state() {
// mtime = 0;
// mtimecmp[0] = 0;
// mtimecmp[1] = 0;
// exit_req = false;
// exit_code = 0;
// monitor_enabled = false;
// for (int i = 0; i < N_RESERVATIONS; ++i) {
// reservation_valid[i] = false;
// reservation_addr[i] = 0;
// }
// mem = new uint8_t[MEM_SIZE];
// for (size_t i = 0; i < MEM_SIZE; ++i)
// mem[i] = 0;
// }
// Where we're going we don't need a destructor B-)
// void step(cxxrtl_design::p_example__soc &tb) {
// // Default update logic for mtime, mtimecmp
// ++mtime;
// // tb.p_timer__irq.set<uint8_t>((mtime >= mtimecmp[0]) | (mtime
// >= mtimecmp[1]) << 1);
// }
// };
// typedef enum {
// SIZE_BYTE = 0,
// SIZE_HWORD = 1,
// SIZE_WORD = 2
// } bus_size_t;
// struct bus_request {
// uint32_t addr;
// bus_size_t size;
// bool write;
// bool excl;
// uint32_t wdata;
// int reservation_id;
// bus_request(): addr(0), size(SIZE_BYTE), write(0), excl(0), wdata(0),
// reservation_id(0) {}
// };
// struct bus_response {
// uint32_t rdata;
// int stall_cycles;
// bool err;
// bool exokay;
// bus_response(): rdata(0), stall_cycles(0), err(false), exokay(true) {}
// };
// bus_response mem_access(cxxrtl_design::p_example__soc &tb, mem_io_state
// &memio, bus_request req) { bus_response resp;
// // Global monitor. When monitor is not enabled, HEXOKAY is tied high
// if (memio.monitor_enabled) {
// if (req.excl) {
// // Always set reservation on read. Always clear
// reservation on
// // write. On successful write, clear others' matching
// reservations. if (req.write) {
// resp.exokay = memio.reservation_valid[req.reservation_id] &&
// memio.reservation_addr[req.reservation_id]
// == (req.addr & RESERVATION_ADDR_MASK);
// memio.reservation_valid[req.reservation_id] =
// false; if (resp.exokay) {
// for (int i = 0; i < N_RESERVATIONS; ++i) {
// if (i == req.reservation_id)
// continue; if
// (memio.reservation_addr[i] == (req.addr & RESERVATION_ADDR_MASK))
// memio.reservation_valid[i] = false;
// }
// }
// }
// else {
// resp.exokay = true;
// memio.reservation_valid[req.reservation_id] =
// true;
// memio.reservation_addr[req.reservation_id] = req.addr &
// RESERVATION_ADDR_MASK;
// }
// }
// else {
// resp.exokay = false;
// // Non-exclusive write still clears others' reservations
// if (req.write) {
// for (int i = 0; i < N_RESERVATIONS; ++i) {
// if (i == req.reservation_id)
// continue;
// if (memio.reservation_addr[i] ==
// (req.addr & RESERVATION_ADDR_MASK))
// memio.reservation_valid[i] = false;
// }
// }
// }
// }
// if (req.write) {
// if (memio.monitor_enabled && req.excl && !resp.exokay) {
// // Failed exclusive write; do nothing
// }
// else if (req.addr <= MEM_SIZE - 4u) {
// unsigned int n_bytes = 1u << (int)req.size;
// // Note we are relying on hazard3's byte lane
// replication for (unsigned int i = 0; i < n_bytes; ++i) {
// memio.mem[req.addr + i] = req.wdata >> (8 * i) & 0xffu;
// }
// }
// else if (req.addr == IO_BASE + IO_PRINT_CHAR) {
// putchar(req.wdata);
// }
// else if (req.addr == IO_BASE + IO_PRINT_U32) {
// printf("%08x\n", req.wdata);
// }
// else if (req.addr == IO_BASE + IO_EXIT) {
// if (!memio.exit_req) {
// memio.exit_req = true;
// memio.exit_code = req.wdata;
// }
// }
// else if (req.addr == IO_BASE + IO_SET_SOFTIRQ) {
// //
// tb.p_soft__irq.set<uint8_t>(tb.p_soft__irq.get<uint8_t>() | req.wdata);
// }
// else if (req.addr == IO_BASE + IO_CLR_SOFTIRQ) {
// //
// tb.p_soft__irq.set<uint8_t>(tb.p_soft__irq.get<uint8_t>() & ~req.wdata);
// }
// else if (req.addr == IO_BASE + IO_GLOBMON_EN) {
// memio.monitor_enabled = req.wdata;
// }
// else if (req.addr == IO_BASE + IO_SET_IRQ) {
// // tb.p_irq.set<uint32_t>(tb.p_irq.get<uint32_t>() |
// req.wdata);
// }
// else if (req.addr == IO_BASE + IO_CLR_IRQ) {
// // tb.p_irq.set<uint32_t>(tb.p_irq.get<uint32_t>() &
// ~req.wdata);
// }
// else if (req.addr == IO_BASE + IO_MTIME) {
// memio.mtime = (memio.mtime & 0xffffffff00000000u) |
// req.wdata;
// }
// else if (req.addr == IO_BASE + IO_MTIMEH) {
// memio.mtime = (memio.mtime & 0x00000000ffffffffu) |
// ((uint64_t)req.wdata << 32);
// }
// else if (req.addr == IO_BASE + IO_MTIMECMP0) {
// memio.mtimecmp[0] = (memio.mtimecmp[0] &
// 0xffffffff00000000u) | req.wdata;
// }
// else if (req.addr == IO_BASE + IO_MTIMECMP0H) {
// memio.mtimecmp[0] = (memio.mtimecmp[0] &
// 0x00000000ffffffffu) | ((uint64_t)req.wdata << 32);
// }
// else if (req.addr == IO_BASE + IO_MTIMECMP1) {
// memio.mtimecmp[1] = (memio.mtimecmp[1] &
// 0xffffffff00000000u) | req.wdata;
// }
// else if (req.addr == IO_BASE + IO_MTIMECMP1H) {
// memio.mtimecmp[1] = (memio.mtimecmp[1] &
// 0x00000000ffffffffu) | ((uint64_t)req.wdata << 32);
// }
// else {
// resp.err = true;
// }
// }
// else {
// if (req.addr <= MEM_SIZE - (1u << (int)req.size)) {
// req.addr &= ~0x3u;
// resp.rdata =
// (uint32_t)memio.mem[req.addr] |
// memio.mem[req.addr + 1] << 8 |
// memio.mem[req.addr + 2] << 16 |
// memio.mem[req.addr + 3] << 24;
// }
// else if (req.addr == IO_BASE + IO_SET_SOFTIRQ || req.addr ==
// IO_BASE + IO_CLR_SOFTIRQ) {
// // resp.rdata = tb.p_soft__irq.get<uint8_t>();
// }
// else if (req.addr == IO_BASE + IO_SET_IRQ || req.addr == IO_BASE
// + IO_CLR_IRQ) {
// // resp.rdata = tb.p_irq.get<uint32_t>();
// }
// else if (req.addr == IO_BASE + IO_MTIME) {
// resp.rdata = memio.mtime;
// }
// else if (req.addr == IO_BASE + IO_MTIMEH) {
// resp.rdata = memio.mtime >> 32;
// }
// else if (req.addr == IO_BASE + IO_MTIMECMP0) {
// resp.rdata = memio.mtimecmp[0];
// }
// else if (req.addr == IO_BASE + IO_MTIMECMP0H) {
// resp.rdata = memio.mtimecmp[0] >> 32;
// }
// else if (req.addr == IO_BASE + IO_MTIMECMP1) {
// resp.rdata = memio.mtimecmp[1];
// }
// else if (req.addr == IO_BASE + IO_MTIMECMP1H) {
// resp.rdata = memio.mtimecmp[1] >> 32;
// }
// else {
// resp.err = true;
// }
// }
// if (resp.err) {
// resp.exokay = false;
// }
// return resp;
// }
// -----------------------------------------------------------------------------
const char *help_str =
@ -425,23 +200,6 @@ int main(int argc, char **argv) {
server_fd, port, (struct sockaddr *)&sock_addr, &sock_addr_len);
}
// mem_io_state memio;
// if (load_bin) {
// std::ifstream fd(bin_path, std::ios::binary | std::ios::ate);
// if (!fd){
// std::cerr << "Failed to open \"" << bin_path << "\"\n";
// return -1;
// }
// std::streamsize bin_size = fd.tellg();
// if (bin_size > MEM_SIZE) {
// std::cerr << "Binary file (" << bin_size << " bytes) is larger
// than memory (" << MEM_SIZE << " bytes)\n"; return -1;
// }
// fd.seekg(0, std::ios::beg);
// fd.read((char*)memio.mem, bin_size);
// }
std::ofstream jtag_dump_fd;
if (dump_jtag) {
jtag_dump_fd.open(jtag_dump_path);
@ -472,20 +230,6 @@ int main(int argc, char **argv) {
vcd.add(all_debug_items);
}
// Loop-carried address-phase requests
// bus_request req_i;
// bus_request req_d;
// bool req_i_vld = false;
// bool req_d_vld = false;
// req_i.reservation_id = 0;
// req_d.reservation_id = 1;
// Set bus interfaces to generate good IDLE responses at first
// top.p_i__hready.set<bool>(true);
// top.p_d__hready.set<bool>(true);
// Reset + initial clock pulse
top.step();
top.p_clk.set<bool>(true);
top.p_tck.set<bool>(true);
@ -591,78 +335,6 @@ int main(int argc, char **argv) {
}
}
// memio.step(top);
// The two bus ports are handled identically. This enables swapping out of
// various `tb.v` hardware integration files containing:
//
// - A single, dual-ported processor (instruction fetch, load/store ports)
// - A single, single-ported processor (instruction fetch + load/store muxed
// internally)
// - A pair of single-ported processors, for dual-core debug tests
// if (top.p_d__hready.get<bool>()) {
// // Clear bus error by default
// top.p_d__hresp.set<bool>(false);
// // Handle current data phase
// req_d.wdata = top.p_d__hwdata.get<uint32_t>();
// bus_response resp;
// if (req_d_vld)
// resp = mem_access(top, memio, req_d);
// else
// resp.exokay = !memio.monitor_enabled;
// if (resp.err) {
// // Phase 1 of error response
// top.p_d__hready.set<bool>(false);
// top.p_d__hresp.set<bool>(true);
// }
// top.p_d__hrdata.set<uint32_t>(resp.rdata);
// top.p_d__hexokay.set<bool>(resp.exokay);
// // Progress current address phase to data phase
// req_d_vld = top.p_d__htrans.get<uint8_t>() >> 1;
// req_d.write = top.p_d__hwrite.get<bool>();
// req_d.size = (bus_size_t)top.p_d__hsize.get<uint8_t>();
// req_d.addr = top.p_d__haddr.get<uint32_t>();
// req_d.excl = top.p_d__hexcl.get<bool>();
// }
// else {
// // hready=0. Currently this only happens when we're in the first
// // phase of an error response, so go to phase 2.
// top.p_d__hready.set<bool>(true);
// }
// if (top.p_i__hready.get<bool>()) {
// top.p_i__hresp.set<bool>(false);
// req_i.wdata = top.p_i__hwdata.get<uint32_t>();
// bus_response resp;
// if (req_i_vld)
// resp = mem_access(top, memio, req_i);
// else
// resp.exokay = !memio.monitor_enabled;
// if (resp.err) {
// // Phase 1 of error response
// top.p_i__hready.set<bool>(false);
// top.p_i__hresp.set<bool>(true);
// }
// top.p_i__hrdata.set<uint32_t>(resp.rdata);
// top.p_i__hexokay.set<bool>(resp.exokay);
// // Progress current address phase to data phase
// req_i_vld = top.p_i__htrans.get<uint8_t>() >> 1;
// req_i.write = top.p_i__hwrite.get<bool>();
// req_i.size = (bus_size_t)top.p_i__hsize.get<uint8_t>();
// req_i.addr = top.p_i__haddr.get<uint32_t>();
// req_i.excl = top.p_i__hexcl.get<bool>();
// }
// else {
// // hready=0. Currently this only happens when we're in the first
// // phase of an error response, so go to phase 2.
// top.p_i__hready.set<bool>(true);
// }
if (dump_waves) {
// The extra step() is just here to get the bus responses to line up
// nicely in the VCD (hopefully is a quick update)
@ -672,11 +344,6 @@ int main(int argc, char **argv) {
vcd.buffer.clear();
}
// if (memio.exit_req) {
// printf("CPU requested halt. Exit code %d\n", memio.exit_code);
// printf("Ran for " I64_FMT " cycles\n", cycle + 1);
// break;
// }
if (cycle + 1 == max_cycles) {
printf("Max cycles reached\n");
timed_out = true;
@ -692,20 +359,9 @@ int main(int argc, char **argv) {
jtag_replay_fd.close();
}
// for (auto r : dump_ranges) {
// printf("Dumping memory from %08x to %08x:\n", r.first, r.second);
// for (int i = 0; i < r.second - r.first; ++i)
// printf("%02x%c", memio.mem[r.first + i], i % 16 == 15 ? '\n' : '
// '); printf("\n");
// }
if (propagate_return_code && timed_out) {
return -1;
}
// else if (propagate_return_code && memio.exit_req) {
// return memio.exit_code;
// }
else {
} else {
return 0;
}
}