Enable aph port off soc, and print prints.

This commit is contained in:
Colin 2025-03-27 23:48:10 +08:00
parent 5ec810907e
commit aaad0d85a5
4 changed files with 664 additions and 629 deletions

View File

@ -28,7 +28,16 @@ module example_soc #(
// IO
output wire uart_tx,
input wire uart_rx
input wire uart_rx,
output wire gp_psel,
output wire gp_penable,
output wire gp_pwrite,
output wire [15:0] gp_paddr,
output wire [31:0] gp_pwdata,
input wire [31:0] gp_prdata,
input wire gp_pready,
input wire gp_pslverr,
);
localparam W_ADDR = 32;
@ -346,9 +355,10 @@ hazard3_cpu_1port #(
// ----------------------------------------------------------------------------
// Bus fabric
// - 128 kB SRAM at... 0x0000_0000
// - System timer at.. 0x4000_0000
// - UART at.......... 0x4000_4000
// - 128 kB SRAM at... 0x0000_0000 Mask: 0xe0000000
// - System timer at.. 0x4000_0000 Mask: 0xe000c000
// - UART at.......... 0x4000_4000 Mask: 0xe000c000
// - GP at.......... 0x4000_8000 Mask: 0xe000c000
// AHBL layer
@ -470,9 +480,9 @@ ahbl_to_apb apb_bridge_u (
);
apb_splitter #(
.N_SLAVES (2),
.ADDR_MAP (32'h4000_0000),
.ADDR_MASK (32'hc000_c000)
.N_SLAVES (3),
.ADDR_MAP (48'h4000_0000_8000),
.ADDR_MASK (48'hc000_c000_c000)
) inst_apb_splitter (
.apbs_paddr (bridge_paddr),
.apbs_psel (bridge_psel),
@ -483,14 +493,14 @@ apb_splitter #(
.apbs_prdata (bridge_prdata),
.apbs_pslverr (bridge_pslverr),
.apbm_paddr ({uart_paddr , timer_paddr }),
.apbm_psel ({uart_psel , timer_psel }),
.apbm_penable ({uart_penable , timer_penable}),
.apbm_pwrite ({uart_pwrite , timer_pwrite }),
.apbm_pwdata ({uart_pwdata , timer_pwdata }),
.apbm_pready ({uart_pready , timer_pready }),
.apbm_prdata ({uart_prdata , timer_prdata }),
.apbm_pslverr ({uart_pslverr , timer_pslverr})
.apbm_paddr ({uart_paddr , timer_paddr, gp_paddr }),
.apbm_psel ({uart_psel , timer_psel, gp_psel }),
.apbm_penable ({uart_penable , timer_penable, gp_penable}),
.apbm_pwrite ({uart_pwrite , timer_pwrite, gp_pwrite }),
.apbm_pwdata ({uart_pwdata , timer_pwdata, gp_pwdata }),
.apbm_pready ({uart_pready , timer_pready, gp_pready }),
.apbm_prdata ({uart_prdata , timer_prdata, gp_prdata }),
.apbm_pslverr ({uart_pslverr , timer_pslverr, gp_pslverr})
);
// ----------------------------------------------------------------------------

View File

@ -9,7 +9,7 @@
// ----------------------------------------------------------------------------
// Testbench IO hardware layout
#define IO_BASE 0x40000000
#define IO_BASE 0x40008000
typedef struct {
volatile uint32_t print_char;

View File

@ -1,6 +1,12 @@
#include "tb_cxxrtl_io.h"
int main() {
tb_puts("Hello world from Hazard3 + CXXRTL!\n");
__attribute__((optimize("O0"))) int main() {
// tb_puts("Hello world from Hazard3 + CXXRTL!\n");
uint32_t addr = 0x40008000;
uint32_t *point = (uint32_t *)addr;
*point = 'C';
*point = 'O';
*point = 'L';
*point = 'I';
*point = 'N';
return 123;
}

View File

@ -1,17 +1,18 @@
#include <iostream>
#include <fstream>
#include <cstdint>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
// Device-under-test model generated by CXXRTL:
#include "dut.cpp"
#include <cxxrtl/cxxrtl_vcd.h>
#include "dut.cpp"
// There must be a better way
#ifdef __x86_64__
#define I64_FMT "%ld"
@ -43,240 +44,266 @@ enum {
IO_MTIMECMP1H = 0x114
};
struct mem_io_state {
uint64_t mtime;
uint64_t mtimecmp[2];
// struct mem_io_state {
// uint64_t mtime;
// uint64_t mtimecmp[2];
bool exit_req;
uint32_t exit_code;
// bool exit_req;
// uint32_t exit_code;
uint8_t *mem;
// uint8_t *mem;
bool monitor_enabled;
bool reservation_valid[2];
uint32_t reservation_addr[2];
// 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;
}
// 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-)
// 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);
}
};
// 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;
// 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_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) {}
};
// 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;
// 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;
}
}
}
}
// // 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;
}
// 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 =
"Usage: tb [--bin x.bin] [--port n] [--vcd x.vcd] [--dump start end] \\\n"
" [--cycles n] [--cpuret] [--jtagdump x] [--jtagreplay x]\n"
"\n"
" --bin x.bin : Flat binary file loaded to address 0x0 in RAM\n"
" --vcd x.vcd : Path to dump waveforms to\n"
" --dump start end : Print out memory contents from start to end (exclusive)\n"
" after execution finishes. Can be passed multiple times.\n"
" --cycles n : Maximum number of cycles to run before exiting.\n"
" Default is 0 (no maximum).\n"
" --port n : Port number to listen for openocd remote bitbang. Sim\n"
" runs in lockstep with JTAG bitbang, not free-running.\n"
" --cpuret : Testbench's return code is the return code written to\n"
" IO_EXIT by the CPU, or -1 if timed out.\n"
" --jtagdump : Dump OpenOCD JTAG bitbang commands to a file so they\n"
" can be replayed. (Lower perf impact than VCD dumping)\n"
" --jtagreplay : Play back some dumped OpenOCD JTAG bitbang commands\n"
;
"Usage: tb [--bin x.bin] [--port n] [--vcd x.vcd] [--dump start end] \\\n"
" [--cycles n] [--cpuret] [--jtagdump x] [--jtagreplay x]\n"
"\n"
" --bin x.bin : Flat binary file loaded to address 0x0 in RAM\n"
" --vcd x.vcd : Path to dump waveforms to\n"
" --dump start end : Print out memory contents from start to end "
"(exclusive)\n"
" after execution finishes. Can be passed multiple "
"times.\n"
" --cycles n : Maximum number of cycles to run before exiting.\n"
" Default is 0 (no maximum).\n"
" --port n : Port number to listen for openocd remote bitbang. "
"Sim\n"
" runs in lockstep with JTAG bitbang, not "
"free-running.\n"
" --cpuret : Testbench's return code is the return code written "
"to\n"
" IO_EXIT by the CPU, or -1 if timed out.\n"
" --jtagdump : Dump OpenOCD JTAG bitbang commands to a file so "
"they\n"
" can be replayed. (Lower perf impact than VCD "
"dumping)\n"
" --jtagreplay : Play back some dumped OpenOCD JTAG bitbang "
"commands\n";
void exit_help(std::string errtext = "") {
std::cerr << errtext << help_str;
exit(-1);
}
int wait_for_connection(int server_fd, uint16_t port, struct sockaddr *sock_addr, socklen_t *sock_addr_len) {
int wait_for_connection(int server_fd, uint16_t port,
struct sockaddr *sock_addr, socklen_t *sock_addr_len) {
int sock_fd;
printf("Waiting for connection on port %u\n", port);
if (listen(server_fd, 3) < 0) {
@ -295,7 +322,6 @@ int wait_for_connection(int server_fd, uint16_t port, struct sockaddr *sock_addr
static const int TCP_BUF_SIZE = 256;
int main(int argc, char **argv) {
bool load_bin = false;
std::string bin_path;
bool dump_waves = false;
@ -314,68 +340,53 @@ int main(int argc, char **argv) {
if (s.rfind("--", 0) != 0) {
std::cerr << "Unexpected positional argument " << s << "\n";
exit_help("");
}
else if (s == "--bin") {
if (argc - i < 2)
exit_help("Option --bin requires an argument\n");
} else if (s == "--bin") {
if (argc - i < 2) exit_help("Option --bin requires an argument\n");
load_bin = true;
bin_path = argv[i + 1];
i += 1;
}
else if (s == "--vcd") {
if (argc - i < 2)
exit_help("Option --vcd requires an argument\n");
} else if (s == "--vcd") {
if (argc - i < 2) exit_help("Option --vcd requires an argument\n");
dump_waves = true;
waves_path = argv[i + 1];
i += 1;
}
else if (s == "--jtagdump") {
if (argc - i < 2)
exit_help("Option --jtagdump requires an argument\n");
} else if (s == "--jtagdump") {
if (argc - i < 2) exit_help("Option --jtagdump requires an argument\n");
dump_jtag = true;
jtag_dump_path = argv[i + 1];
i += 1;
}
else if (s == "--jtagreplay") {
if (argc - i < 2)
exit_help("Option --jtagreplay requires an argument\n");
} else if (s == "--jtagreplay") {
if (argc - i < 2) exit_help("Option --jtagreplay requires an argument\n");
replay_jtag = true;
jtag_replay_path = argv[i + 1];
i += 1;
}
else if (s == "--dump") {
if (argc - i < 3)
exit_help("Option --dump requires 2 arguments\n");
} else if (s == "--dump") {
if (argc - i < 3) exit_help("Option --dump requires 2 arguments\n");
dump_ranges.push_back(std::pair<uint32_t, uint32_t>(
std::stoul(argv[i + 1], 0, 0),
std::stoul(argv[i + 2], 0, 0)
));;
std::stoul(argv[i + 1], 0, 0), std::stoul(argv[i + 2], 0, 0)));
;
i += 2;
}
else if (s == "--cycles") {
if (argc - i < 2)
exit_help("Option --cycles requires an argument\n");
} else if (s == "--cycles") {
if (argc - i < 2) exit_help("Option --cycles requires an argument\n");
max_cycles = std::stol(argv[i + 1], 0, 0);
i += 1;
}
else if (s == "--port") {
if (argc - i < 2)
exit_help("Option --port requires an argument\n");
} else if (s == "--port") {
if (argc - i < 2) exit_help("Option --port requires an argument\n");
port = std::stol(argv[i + 1], 0, 0);
i += 1;
}
else if (s == "--cpuret") {
} else if (s == "--cpuret") {
propagate_return_code = true;
}
else {
} else {
std::cerr << "Unrecognised argument " << s << "\n";
exit_help("");
}
}
if (!(load_bin || port != 0 || replay_jtag))
exit_help("At least one of --bin, --port or --jtagreplay must be specified.\n");
exit_help(
"At least one of --bin, --port or --jtagreplay must be specified.\n");
if (dump_jtag && port == 0)
exit_help("--jtagdump specified, but there is no JTAG socket to dump from.\n");
exit_help(
"--jtagdump specified, but there is no JTAG socket to dump from.\n");
if (replay_jtag && port != 0)
exit_help("Can't specify both --port and --jtagreplay\n");
@ -393,10 +404,9 @@ int main(int argc, char **argv) {
exit(-1);
}
int setsockopt_rc = setsockopt(
server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&sock_opt, sizeof(sock_opt)
);
int setsockopt_rc =
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&sock_opt, sizeof(sock_opt));
if (setsockopt_rc) {
fprintf(stderr, "setsockopt failed\n");
@ -411,25 +421,26 @@ int main(int argc, char **argv) {
exit(-1);
}
sock_fd = wait_for_connection(server_fd, port, (struct sockaddr *)&sock_addr, &sock_addr_len);
sock_fd = wait_for_connection(
server_fd, port, (struct sockaddr *)&sock_addr, &sock_addr_len);
}
mem_io_state memio;
// 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);
}
// 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) {
@ -462,12 +473,12 @@ int main(int argc, char **argv) {
}
// 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;
// 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);
@ -483,6 +494,11 @@ int main(int argc, char **argv) {
top.p_tck.set<bool>(false);
top.p_trst__n.set<bool>(true);
top.p_rst__n.set<bool>(true);
top.p_gp__pready.set<bool>(true);
top.p_gp__pslverr.set<bool>(false);
top.p_gp__prdata.set<uint32_t>(0);
top.step();
top.step(); // workaround for github.com/YosysHQ/yosys/issues/2780
@ -490,9 +506,9 @@ int main(int argc, char **argv) {
for (int64_t cycle = 0; cycle < max_cycles || max_cycles == 0; ++cycle) {
top.p_clk.set<bool>(false);
top.step();
if (dump_waves)
vcd.sample(cycle * 2);
if (dump_waves) vcd.sample(cycle * 2);
top.p_clk.set<bool>(true);
top.step();
top.step(); // workaround for github.com/YosysHQ/yosys/issues/2780
@ -514,31 +530,26 @@ int main(int argc, char **argv) {
if (c == 'r' || c == 's') {
top.p_trst__n.set<bool>(true);
step = true;
}
else if (c == 't' || c == 'u') {
} else if (c == 't' || c == 'u') {
top.p_trst__n.set<bool>(false);
}
else if (c >= '0' && c <= '7') {
} else if (c >= '0' && c <= '7') {
int mask = c - '0';
top.p_tck.set<bool>(mask & 0x4);
top.p_tms.set<bool>(mask & 0x2);
top.p_tdi.set<bool>(mask & 0x1);
step = true;
}
else if (c == 'R') {
} else if (c == 'R') {
txbuf[tx_ptr++] = top.p_tdo.get<bool>() ? '1' : '0';
if (tx_ptr >= TCP_BUF_SIZE || rx_remaining == 0) {
send(sock_fd, txbuf, tx_ptr, 0);
tx_ptr = 0;
}
}
else if (c == 'Q') {
} else if (c == 'Q') {
printf("OpenOCD sent quit command\n");
got_exit_cmd = true;
step = true;
}
}
else {
} else {
// Potentially the last command was not a read command, but
// OpenOCD is still waiting for a last response from its
// last command packet before it sends us any more, so now is
@ -550,8 +561,7 @@ int main(int argc, char **argv) {
rx_ptr = 0;
if (replay_jtag) {
rx_remaining = jtag_replay_fd.readsome(rxbuf, TCP_BUF_SIZE);
}
else {
} else {
rx_remaining = read(sock_fd, &rxbuf, TCP_BUF_SIZE);
}
if (dump_jtag && rx_remaining > 0) {
@ -561,23 +571,34 @@ int main(int argc, char **argv) {
if (port == 0) {
// Presumably EOF, so quit.
got_exit_cmd = true;
}
else {
} else {
// The socket is closed. Wait for another connection.
sock_fd = wait_for_connection(server_fd, port, (struct sockaddr *)&sock_addr, &sock_addr_len);
sock_fd = wait_for_connection(server_fd, port,
(struct sockaddr *)&sock_addr,
&sock_addr_len);
}
}
}
}
}
memio.step(top);
if (top.p_gp__psel.get<bool>() && top.p_gp__penable.get<bool>() &&
top.p_gp__pwrite.get<bool>()) {
if (top.p_gp__paddr.get<uint16_t>() == 0x8000) {
uint32_t data = top.p_gp__pwdata.get<uint32_t>();
uint8_t c = (data & 0xff);
printf("IO_PRINT_CHAR: %c\n", (char)c);
}
}
// 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 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>()) {
@ -612,7 +633,6 @@ int main(int argc, char **argv) {
// top.p_d__hready.set<bool>(true);
// }
// if (top.p_i__hready.get<bool>()) {
// top.p_i__hresp.set<bool>(false);
@ -644,25 +664,24 @@ int main(int argc, char **argv) {
// }
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)
// The extra step() is just here to get the bus responses to line up
// nicely in the VCD (hopefully is a quick update)
top.step();
vcd.sample(cycle * 2 + 1);
waves_fd << vcd.buffer;
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 (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;
}
if (got_exit_cmd)
break;
if (got_exit_cmd) break;
}
close(sock_fd);
@ -673,19 +692,19 @@ 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");
}
// 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 if (propagate_return_code && memio.exit_req) {
// return memio.exit_code;
// }
else {
return 0;
}