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

View File

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

View File

@ -1,6 +1,12 @@
#include "tb_cxxrtl_io.h" #include "tb_cxxrtl_io.h"
__attribute__((optimize("O0"))) int main() {
int main() { // tb_puts("Hello world from Hazard3 + CXXRTL!\n");
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; 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 <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: // Device-under-test model generated by CXXRTL:
#include "dut.cpp"
#include <cxxrtl/cxxrtl_vcd.h> #include <cxxrtl/cxxrtl_vcd.h>
#include "dut.cpp"
// There must be a better way // There must be a better way
#ifdef __x86_64__ #ifdef __x86_64__
#define I64_FMT "%ld" #define I64_FMT "%ld"
@ -43,212 +44,230 @@ enum {
IO_MTIMECMP1H = 0x114 IO_MTIMECMP1H = 0x114
}; };
struct mem_io_state { // struct mem_io_state {
uint64_t mtime; // uint64_t mtime;
uint64_t mtimecmp[2]; // uint64_t mtimecmp[2];
bool exit_req; // bool exit_req;
uint32_t exit_code; // uint32_t exit_code;
uint8_t *mem; // uint8_t *mem;
bool monitor_enabled; // bool monitor_enabled;
bool reservation_valid[2]; // bool reservation_valid[2];
uint32_t reservation_addr[2]; // uint32_t reservation_addr[2];
mem_io_state() { // mem_io_state() {
mtime = 0; // mtime = 0;
mtimecmp[0] = 0; // mtimecmp[0] = 0;
mtimecmp[1] = 0; // mtimecmp[1] = 0;
exit_req = false; // exit_req = false;
exit_code = 0; // exit_code = 0;
monitor_enabled = false; // monitor_enabled = false;
for (int i = 0; i < N_RESERVATIONS; ++i) { // for (int i = 0; i < N_RESERVATIONS; ++i) {
reservation_valid[i] = false; // reservation_valid[i] = false;
reservation_addr[i] = 0; // reservation_addr[i] = 0;
} // }
mem = new uint8_t[MEM_SIZE]; // mem = new uint8_t[MEM_SIZE];
for (size_t i = 0; i < MEM_SIZE; ++i) // for (size_t i = 0; i < MEM_SIZE; ++i)
mem[i] = 0; // 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) { // void step(cxxrtl_design::p_example__soc &tb) {
// Default update logic for mtime, mtimecmp // // Default update logic for mtime, mtimecmp
++mtime; // ++mtime;
// tb.p_timer__irq.set<uint8_t>((mtime >= mtimecmp[0]) | (mtime >= mtimecmp[1]) << 1); // // tb.p_timer__irq.set<uint8_t>((mtime >= mtimecmp[0]) | (mtime
} // >= mtimecmp[1]) << 1);
}; // }
// };
typedef enum { // typedef enum {
SIZE_BYTE = 0, // SIZE_BYTE = 0,
SIZE_HWORD = 1, // SIZE_HWORD = 1,
SIZE_WORD = 2 // SIZE_WORD = 2
} bus_size_t; // } bus_size_t;
struct bus_request { // struct bus_request {
uint32_t addr; // uint32_t addr;
bus_size_t size; // bus_size_t size;
bool write; // bool write;
bool excl; // bool excl;
uint32_t wdata; // uint32_t wdata;
int reservation_id; // int reservation_id;
bus_request(): addr(0), size(SIZE_BYTE), write(0), excl(0), wdata(0), reservation_id(0) {} // bus_request(): addr(0), size(SIZE_BYTE), write(0), excl(0), wdata(0),
}; // reservation_id(0) {}
// };
struct bus_response { // struct bus_response {
uint32_t rdata; // uint32_t rdata;
int stall_cycles; // int stall_cycles;
bool err; // bool err;
bool exokay; // bool exokay;
bus_response(): rdata(0), stall_cycles(0), err(false), exokay(true) {} // 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 mem_access(cxxrtl_design::p_example__soc &tb, mem_io_state
bus_response resp; // &memio, bus_request req) { bus_response resp;
// Global monitor. When monitor is not enabled, HEXOKAY is tied high // // Global monitor. When monitor is not enabled, HEXOKAY is tied high
if (memio.monitor_enabled) { // if (memio.monitor_enabled) {
if (req.excl) { // if (req.excl) {
// Always set reservation on read. Always clear reservation on // // Always set reservation on read. Always clear
// write. On successful write, clear others' matching reservations. // reservation on
if (req.write) { // // write. On successful write, clear others' matching
resp.exokay = memio.reservation_valid[req.reservation_id] && // reservations. if (req.write) {
memio.reservation_addr[req.reservation_id] == (req.addr & RESERVATION_ADDR_MASK); // resp.exokay = memio.reservation_valid[req.reservation_id] &&
memio.reservation_valid[req.reservation_id] = false; // memio.reservation_addr[req.reservation_id]
if (resp.exokay) { // == (req.addr & RESERVATION_ADDR_MASK);
for (int i = 0; i < N_RESERVATIONS; ++i) { // memio.reservation_valid[req.reservation_id] =
if (i == req.reservation_id) // false; if (resp.exokay) {
continue; // for (int i = 0; i < N_RESERVATIONS; ++i) {
if (memio.reservation_addr[i] == (req.addr & RESERVATION_ADDR_MASK)) // if (i == req.reservation_id)
memio.reservation_valid[i] = false; // 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; // else {
memio.reservation_addr[req.reservation_id] = req.addr & RESERVATION_ADDR_MASK; // resp.exokay = true;
} // memio.reservation_valid[req.reservation_id] =
} // true;
else { // memio.reservation_addr[req.reservation_id] = req.addr &
resp.exokay = false; // RESERVATION_ADDR_MASK;
// Non-exclusive write still clears others' reservations // }
if (req.write) { // }
for (int i = 0; i < N_RESERVATIONS; ++i) { // else {
if (i == req.reservation_id) // resp.exokay = false;
continue; // // Non-exclusive write still clears others' reservations
if (memio.reservation_addr[i] == (req.addr & RESERVATION_ADDR_MASK)) // if (req.write) {
memio.reservation_valid[i] = false; // 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 (req.write) { // if (memio.monitor_enabled && req.excl && !resp.exokay) {
if (memio.monitor_enabled && req.excl && !resp.exokay) { // // Failed exclusive write; do nothing
// Failed exclusive write; do nothing // }
} // else if (req.addr <= MEM_SIZE - 4u) {
else if (req.addr <= MEM_SIZE - 4u) { // unsigned int n_bytes = 1u << (int)req.size;
unsigned int n_bytes = 1u << (int)req.size; // // Note we are relying on hazard3's byte lane
// Note we are relying on hazard3's byte lane replication // replication for (unsigned int i = 0; i < n_bytes; ++i) {
for (unsigned int i = 0; i < n_bytes; ++i) { // memio.mem[req.addr + i] = req.wdata >> (8 * i) & 0xffu;
memio.mem[req.addr + i] = req.wdata >> (8 * i) & 0xffu; // }
} // }
} // else if (req.addr == IO_BASE + IO_PRINT_CHAR) {
else if (req.addr == IO_BASE + IO_PRINT_CHAR) { // putchar(req.wdata);
putchar(req.wdata); // }
} // else if (req.addr == IO_BASE + IO_PRINT_U32) {
else if (req.addr == IO_BASE + IO_PRINT_U32) { // printf("%08x\n", req.wdata);
printf("%08x\n", req.wdata); // }
} // else if (req.addr == IO_BASE + IO_EXIT) {
else if (req.addr == IO_BASE + IO_EXIT) { // if (!memio.exit_req) {
if (!memio.exit_req) { // memio.exit_req = true;
memio.exit_req = true; // memio.exit_code = req.wdata;
memio.exit_code = req.wdata; // }
} // }
} // else if (req.addr == IO_BASE + IO_SET_SOFTIRQ) {
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); // 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) { // 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); // 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) { // else if (req.addr == IO_BASE + IO_GLOBMON_EN) {
memio.monitor_enabled = req.wdata; // memio.monitor_enabled = req.wdata;
} // }
else if (req.addr == IO_BASE + IO_SET_IRQ) { // else if (req.addr == IO_BASE + IO_SET_IRQ) {
// tb.p_irq.set<uint32_t>(tb.p_irq.get<uint32_t>() | req.wdata); // // 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_CLR_IRQ) {
} // // tb.p_irq.set<uint32_t>(tb.p_irq.get<uint32_t>() &
else if (req.addr == IO_BASE + IO_MTIME) { // ~req.wdata);
memio.mtime = (memio.mtime & 0xffffffff00000000u) | req.wdata; // }
} // else if (req.addr == IO_BASE + IO_MTIME) {
else if (req.addr == IO_BASE + IO_MTIMEH) { // memio.mtime = (memio.mtime & 0xffffffff00000000u) |
memio.mtime = (memio.mtime & 0x00000000ffffffffu) | ((uint64_t)req.wdata << 32); // req.wdata;
} // }
else if (req.addr == IO_BASE + IO_MTIMECMP0) { // else if (req.addr == IO_BASE + IO_MTIMEH) {
memio.mtimecmp[0] = (memio.mtimecmp[0] & 0xffffffff00000000u) | req.wdata; // memio.mtime = (memio.mtime & 0x00000000ffffffffu) |
} // ((uint64_t)req.wdata << 32);
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_MTIMECMP0) {
} // memio.mtimecmp[0] = (memio.mtimecmp[0] &
else if (req.addr == IO_BASE + IO_MTIMECMP1) { // 0xffffffff00000000u) | req.wdata;
memio.mtimecmp[1] = (memio.mtimecmp[1] & 0xffffffff00000000u) | req.wdata; // }
} // else if (req.addr == IO_BASE + IO_MTIMECMP0H) {
else if (req.addr == IO_BASE + IO_MTIMECMP1H) { // memio.mtimecmp[0] = (memio.mtimecmp[0] &
memio.mtimecmp[1] = (memio.mtimecmp[1] & 0x00000000ffffffffu) | ((uint64_t)req.wdata << 32); // 0x00000000ffffffffu) | ((uint64_t)req.wdata << 32);
} // }
else { // else if (req.addr == IO_BASE + IO_MTIMECMP1) {
resp.err = true; // memio.mtimecmp[1] = (memio.mtimecmp[1] &
} // 0xffffffff00000000u) | req.wdata;
} // }
else { // else if (req.addr == IO_BASE + IO_MTIMECMP1H) {
if (req.addr <= MEM_SIZE - (1u << (int)req.size)) { // memio.mtimecmp[1] = (memio.mtimecmp[1] &
req.addr &= ~0x3u; // 0x00000000ffffffffu) | ((uint64_t)req.wdata << 32);
resp.rdata = // }
(uint32_t)memio.mem[req.addr] | // else {
memio.mem[req.addr + 1] << 8 | // resp.err = true;
memio.mem[req.addr + 2] << 16 | // }
memio.mem[req.addr + 3] << 24; // }
} // else {
else if (req.addr == IO_BASE + IO_SET_SOFTIRQ || req.addr == IO_BASE + IO_CLR_SOFTIRQ) { // if (req.addr <= MEM_SIZE - (1u << (int)req.size)) {
// resp.rdata = tb.p_soft__irq.get<uint8_t>(); // req.addr &= ~0x3u;
} // resp.rdata =
else if (req.addr == IO_BASE + IO_SET_IRQ || req.addr == IO_BASE + IO_CLR_IRQ) { // (uint32_t)memio.mem[req.addr] |
// resp.rdata = tb.p_irq.get<uint32_t>(); // memio.mem[req.addr + 1] << 8 |
} // memio.mem[req.addr + 2] << 16 |
else if (req.addr == IO_BASE + IO_MTIME) { // memio.mem[req.addr + 3] << 24;
resp.rdata = memio.mtime; // }
} // else if (req.addr == IO_BASE + IO_SET_SOFTIRQ || req.addr ==
else if (req.addr == IO_BASE + IO_MTIMEH) { // IO_BASE + IO_CLR_SOFTIRQ) {
resp.rdata = memio.mtime >> 32; // // resp.rdata = tb.p_soft__irq.get<uint8_t>();
} // }
else if (req.addr == IO_BASE + IO_MTIMECMP0) { // else if (req.addr == IO_BASE + IO_SET_IRQ || req.addr == IO_BASE
resp.rdata = memio.mtimecmp[0]; // + IO_CLR_IRQ) {
} // // resp.rdata = tb.p_irq.get<uint32_t>();
else if (req.addr == IO_BASE + IO_MTIMECMP0H) { // }
resp.rdata = memio.mtimecmp[0] >> 32; // else if (req.addr == IO_BASE + IO_MTIME) {
} // resp.rdata = memio.mtime;
else if (req.addr == IO_BASE + IO_MTIMECMP1) { // }
resp.rdata = memio.mtimecmp[1]; // else if (req.addr == IO_BASE + IO_MTIMEH) {
} // resp.rdata = memio.mtime >> 32;
else if (req.addr == IO_BASE + IO_MTIMECMP1H) { // }
resp.rdata = memio.mtimecmp[1] >> 32; // else if (req.addr == IO_BASE + IO_MTIMECMP0) {
} // resp.rdata = memio.mtimecmp[0];
else { // }
resp.err = true; // else if (req.addr == IO_BASE + IO_MTIMECMP0H) {
} // resp.rdata = memio.mtimecmp[0] >> 32;
} // }
if (resp.err) { // else if (req.addr == IO_BASE + IO_MTIMECMP1) {
resp.exokay = false; // resp.rdata = memio.mtimecmp[1];
} // }
return resp; // 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;
// }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -258,25 +277,33 @@ const char *help_str =
"\n" "\n"
" --bin x.bin : Flat binary file loaded to address 0x0 in RAM\n" " --bin x.bin : Flat binary file loaded to address 0x0 in RAM\n"
" --vcd x.vcd : Path to dump waveforms to\n" " --vcd x.vcd : Path to dump waveforms to\n"
" --dump start end : Print out memory contents from start to end (exclusive)\n" " --dump start end : Print out memory contents from start to end "
" after execution finishes. Can be passed multiple times.\n" "(exclusive)\n"
" after execution finishes. Can be passed multiple "
"times.\n"
" --cycles n : Maximum number of cycles to run before exiting.\n" " --cycles n : Maximum number of cycles to run before exiting.\n"
" Default is 0 (no maximum).\n" " Default is 0 (no maximum).\n"
" --port n : Port number to listen for openocd remote bitbang. Sim\n" " --port n : Port number to listen for openocd remote bitbang. "
" runs in lockstep with JTAG bitbang, not free-running.\n" "Sim\n"
" --cpuret : Testbench's return code is the return code written to\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" " IO_EXIT by the CPU, or -1 if timed out.\n"
" --jtagdump : Dump OpenOCD JTAG bitbang commands to a file so they\n" " --jtagdump : Dump OpenOCD JTAG bitbang commands to a file so "
" can be replayed. (Lower perf impact than VCD dumping)\n" "they\n"
" --jtagreplay : Play back some dumped OpenOCD JTAG bitbang commands\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 = "") { void exit_help(std::string errtext = "") {
std::cerr << errtext << help_str; std::cerr << errtext << help_str;
exit(-1); 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; int sock_fd;
printf("Waiting for connection on port %u\n", port); printf("Waiting for connection on port %u\n", port);
if (listen(server_fd, 3) < 0) { 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; static const int TCP_BUF_SIZE = 256;
int main(int argc, char **argv) { int main(int argc, char **argv) {
bool load_bin = false; bool load_bin = false;
std::string bin_path; std::string bin_path;
bool dump_waves = false; bool dump_waves = false;
@ -314,68 +340,53 @@ int main(int argc, char **argv) {
if (s.rfind("--", 0) != 0) { if (s.rfind("--", 0) != 0) {
std::cerr << "Unexpected positional argument " << s << "\n"; std::cerr << "Unexpected positional argument " << s << "\n";
exit_help(""); exit_help("");
} } else if (s == "--bin") {
else if (s == "--bin") { if (argc - i < 2) exit_help("Option --bin requires an argument\n");
if (argc - i < 2)
exit_help("Option --bin requires an argument\n");
load_bin = true; load_bin = true;
bin_path = argv[i + 1]; bin_path = argv[i + 1];
i += 1; i += 1;
} } else if (s == "--vcd") {
else if (s == "--vcd") { if (argc - i < 2) exit_help("Option --vcd requires an argument\n");
if (argc - i < 2)
exit_help("Option --vcd requires an argument\n");
dump_waves = true; dump_waves = true;
waves_path = argv[i + 1]; waves_path = argv[i + 1];
i += 1; i += 1;
} } else if (s == "--jtagdump") {
else if (s == "--jtagdump") { if (argc - i < 2) exit_help("Option --jtagdump requires an argument\n");
if (argc - i < 2)
exit_help("Option --jtagdump requires an argument\n");
dump_jtag = true; dump_jtag = true;
jtag_dump_path = argv[i + 1]; jtag_dump_path = argv[i + 1];
i += 1; i += 1;
} } else if (s == "--jtagreplay") {
else if (s == "--jtagreplay") { if (argc - i < 2) exit_help("Option --jtagreplay requires an argument\n");
if (argc - i < 2)
exit_help("Option --jtagreplay requires an argument\n");
replay_jtag = true; replay_jtag = true;
jtag_replay_path = argv[i + 1]; jtag_replay_path = argv[i + 1];
i += 1; i += 1;
} } else if (s == "--dump") {
else if (s == "--dump") { if (argc - i < 3) exit_help("Option --dump requires 2 arguments\n");
if (argc - i < 3)
exit_help("Option --dump requires 2 arguments\n");
dump_ranges.push_back(std::pair<uint32_t, uint32_t>( dump_ranges.push_back(std::pair<uint32_t, uint32_t>(
std::stoul(argv[i + 1], 0, 0), std::stoul(argv[i + 1], 0, 0), std::stoul(argv[i + 2], 0, 0)));
std::stoul(argv[i + 2], 0, 0) ;
));;
i += 2; i += 2;
} } else if (s == "--cycles") {
else if (s == "--cycles") { if (argc - i < 2) exit_help("Option --cycles requires an argument\n");
if (argc - i < 2)
exit_help("Option --cycles requires an argument\n");
max_cycles = std::stol(argv[i + 1], 0, 0); max_cycles = std::stol(argv[i + 1], 0, 0);
i += 1; i += 1;
} } else if (s == "--port") {
else if (s == "--port") { if (argc - i < 2) exit_help("Option --port requires an argument\n");
if (argc - i < 2)
exit_help("Option --port requires an argument\n");
port = std::stol(argv[i + 1], 0, 0); port = std::stol(argv[i + 1], 0, 0);
i += 1; i += 1;
} } else if (s == "--cpuret") {
else if (s == "--cpuret") {
propagate_return_code = true; propagate_return_code = true;
} } else {
else {
std::cerr << "Unrecognised argument " << s << "\n"; std::cerr << "Unrecognised argument " << s << "\n";
exit_help(""); exit_help("");
} }
} }
if (!(load_bin || port != 0 || replay_jtag)) 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) 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) if (replay_jtag && port != 0)
exit_help("Can't specify both --port and --jtagreplay\n"); exit_help("Can't specify both --port and --jtagreplay\n");
@ -393,10 +404,9 @@ int main(int argc, char **argv) {
exit(-1); exit(-1);
} }
int setsockopt_rc = setsockopt( int setsockopt_rc =
server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&sock_opt, sizeof(sock_opt) &sock_opt, sizeof(sock_opt));
);
if (setsockopt_rc) { if (setsockopt_rc) {
fprintf(stderr, "setsockopt failed\n"); fprintf(stderr, "setsockopt failed\n");
@ -411,25 +421,26 @@ int main(int argc, char **argv) {
exit(-1); 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) { // if (load_bin) {
std::ifstream fd(bin_path, std::ios::binary | std::ios::ate); // std::ifstream fd(bin_path, std::ios::binary | std::ios::ate);
if (!fd){ // if (!fd){
std::cerr << "Failed to open \"" << bin_path << "\"\n"; // std::cerr << "Failed to open \"" << bin_path << "\"\n";
return -1; // return -1;
} // }
std::streamsize bin_size = fd.tellg(); // std::streamsize bin_size = fd.tellg();
if (bin_size > MEM_SIZE) { // if (bin_size > MEM_SIZE) {
std::cerr << "Binary file (" << bin_size << " bytes) is larger than memory (" << MEM_SIZE << " bytes)\n"; // std::cerr << "Binary file (" << bin_size << " bytes) is larger
return -1; // than memory (" << MEM_SIZE << " bytes)\n"; return -1;
} // }
fd.seekg(0, std::ios::beg); // fd.seekg(0, std::ios::beg);
fd.read((char*)memio.mem, bin_size); // fd.read((char*)memio.mem, bin_size);
} // }
std::ofstream jtag_dump_fd; std::ofstream jtag_dump_fd;
if (dump_jtag) { if (dump_jtag) {
@ -462,12 +473,12 @@ int main(int argc, char **argv) {
} }
// Loop-carried address-phase requests // Loop-carried address-phase requests
bus_request req_i; // bus_request req_i;
bus_request req_d; // bus_request req_d;
bool req_i_vld = false; // bool req_i_vld = false;
bool req_d_vld = false; // bool req_d_vld = false;
req_i.reservation_id = 0; // req_i.reservation_id = 0;
req_d.reservation_id = 1; // req_d.reservation_id = 1;
// Set bus interfaces to generate good IDLE responses at first // Set bus interfaces to generate good IDLE responses at first
// top.p_i__hready.set<bool>(true); // 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_tck.set<bool>(false);
top.p_trst__n.set<bool>(true); top.p_trst__n.set<bool>(true);
top.p_rst__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();
top.step(); // workaround for github.com/YosysHQ/yosys/issues/2780 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) { for (int64_t cycle = 0; cycle < max_cycles || max_cycles == 0; ++cycle) {
top.p_clk.set<bool>(false); top.p_clk.set<bool>(false);
top.step(); top.step();
if (dump_waves) if (dump_waves) vcd.sample(cycle * 2);
vcd.sample(cycle * 2);
top.p_clk.set<bool>(true); top.p_clk.set<bool>(true);
top.step(); top.step();
top.step(); // workaround for github.com/YosysHQ/yosys/issues/2780 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') { if (c == 'r' || c == 's') {
top.p_trst__n.set<bool>(true); top.p_trst__n.set<bool>(true);
step = true; step = true;
} } else if (c == 't' || c == 'u') {
else if (c == 't' || c == 'u') {
top.p_trst__n.set<bool>(false); top.p_trst__n.set<bool>(false);
} } else if (c >= '0' && c <= '7') {
else if (c >= '0' && c <= '7') {
int mask = c - '0'; int mask = c - '0';
top.p_tck.set<bool>(mask & 0x4); top.p_tck.set<bool>(mask & 0x4);
top.p_tms.set<bool>(mask & 0x2); top.p_tms.set<bool>(mask & 0x2);
top.p_tdi.set<bool>(mask & 0x1); top.p_tdi.set<bool>(mask & 0x1);
step = true; step = true;
} } else if (c == 'R') {
else if (c == 'R') {
txbuf[tx_ptr++] = top.p_tdo.get<bool>() ? '1' : '0'; txbuf[tx_ptr++] = top.p_tdo.get<bool>() ? '1' : '0';
if (tx_ptr >= TCP_BUF_SIZE || rx_remaining == 0) { if (tx_ptr >= TCP_BUF_SIZE || rx_remaining == 0) {
send(sock_fd, txbuf, tx_ptr, 0); send(sock_fd, txbuf, tx_ptr, 0);
tx_ptr = 0; tx_ptr = 0;
} }
} } else if (c == 'Q') {
else if (c == 'Q') {
printf("OpenOCD sent quit command\n"); printf("OpenOCD sent quit command\n");
got_exit_cmd = true; got_exit_cmd = true;
step = true; step = true;
} }
} } else {
else {
// Potentially the last command was not a read command, but // Potentially the last command was not a read command, but
// OpenOCD is still waiting for a last response from its // OpenOCD is still waiting for a last response from its
// last command packet before it sends us any more, so now is // 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; rx_ptr = 0;
if (replay_jtag) { if (replay_jtag) {
rx_remaining = jtag_replay_fd.readsome(rxbuf, TCP_BUF_SIZE); rx_remaining = jtag_replay_fd.readsome(rxbuf, TCP_BUF_SIZE);
} } else {
else {
rx_remaining = read(sock_fd, &rxbuf, TCP_BUF_SIZE); rx_remaining = read(sock_fd, &rxbuf, TCP_BUF_SIZE);
} }
if (dump_jtag && rx_remaining > 0) { if (dump_jtag && rx_remaining > 0) {
@ -561,23 +571,34 @@ int main(int argc, char **argv) {
if (port == 0) { if (port == 0) {
// Presumably EOF, so quit. // Presumably EOF, so quit.
got_exit_cmd = true; got_exit_cmd = true;
} } else {
else {
// The socket is closed. Wait for another connection. // 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 // The two bus ports are handled identically. This enables swapping out of
// various `tb.v` hardware integration files containing: // various `tb.v` hardware integration files containing:
// //
// - A single, dual-ported processor (instruction fetch, load/store ports) // - 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 // - A pair of single-ported processors, for dual-core debug tests
// if (top.p_d__hready.get<bool>()) { // if (top.p_d__hready.get<bool>()) {
@ -612,7 +633,6 @@ int main(int argc, char **argv) {
// top.p_d__hready.set<bool>(true); // top.p_d__hready.set<bool>(true);
// } // }
// if (top.p_i__hready.get<bool>()) { // if (top.p_i__hready.get<bool>()) {
// top.p_i__hresp.set<bool>(false); // top.p_i__hresp.set<bool>(false);
@ -644,25 +664,24 @@ int main(int argc, char **argv) {
// } // }
if (dump_waves) { if (dump_waves) {
// The extra step() is just here to get the bus responses to line up nicely // The extra step() is just here to get the bus responses to line up
// in the VCD (hopefully is a quick update) // nicely in the VCD (hopefully is a quick update)
top.step(); top.step();
vcd.sample(cycle * 2 + 1); vcd.sample(cycle * 2 + 1);
waves_fd << vcd.buffer; waves_fd << vcd.buffer;
vcd.buffer.clear(); vcd.buffer.clear();
} }
if (memio.exit_req) { // if (memio.exit_req) {
printf("CPU requested halt. Exit code %d\n", memio.exit_code); // printf("CPU requested halt. Exit code %d\n", memio.exit_code);
printf("Ran for " I64_FMT " cycles\n", cycle + 1); // printf("Ran for " I64_FMT " cycles\n", cycle + 1);
break; // break;
} // }
if (cycle + 1 == max_cycles) { if (cycle + 1 == max_cycles) {
printf("Max cycles reached\n"); printf("Max cycles reached\n");
timed_out = true; timed_out = true;
} }
if (got_exit_cmd) if (got_exit_cmd) break;
break;
} }
close(sock_fd); close(sock_fd);
@ -673,19 +692,19 @@ int main(int argc, char **argv) {
jtag_replay_fd.close(); jtag_replay_fd.close();
} }
for (auto r : dump_ranges) { // for (auto r : dump_ranges) {
printf("Dumping memory from %08x to %08x:\n", r.first, r.second); // printf("Dumping memory from %08x to %08x:\n", r.first, r.second);
for (int i = 0; i < r.second - r.first; ++i) // for (int i = 0; i < r.second - r.first; ++i)
printf("%02x%c", memio.mem[r.first + i], i % 16 == 15 ? '\n' : ' '); // printf("%02x%c", memio.mem[r.first + i], i % 16 == 15 ? '\n' : '
printf("\n"); // '); printf("\n");
} // }
if (propagate_return_code && timed_out) { if (propagate_return_code && timed_out) {
return -1; return -1;
} }
else if (propagate_return_code && memio.exit_req) { // else if (propagate_return_code && memio.exit_req) {
return memio.exit_code; // return memio.exit_code;
} // }
else { else {
return 0; return 0;
} }