From 551822916c593dd79ee219416fa98aa077a25eb7 Mon Sep 17 00:00:00 2001 From: Eyck Jentzsch Date: Fri, 14 Jun 2024 17:43:12 +0200 Subject: [PATCH] applies clang-format --- src/iss/arch/riscv_hart_mu_p.h | 2 +- src/iss/semihosting/semihosting.cpp | 123 ++++++++++++++-------------- src/iss/semihosting/semihosting.h | 9 +- src/main.cpp | 4 +- 4 files changed, 70 insertions(+), 68 deletions(-) diff --git a/src/iss/arch/riscv_hart_mu_p.h b/src/iss/arch/riscv_hart_mu_p.h index b20f17a..5928d4e 100644 --- a/src/iss/arch/riscv_hart_mu_p.h +++ b/src/iss/arch/riscv_hart_mu_p.h @@ -380,7 +380,7 @@ protected: bool tohost_lower_written = false; riscv_instrumentation_if instr_if; - semihosting_cb_t semihosting_cb; + semihosting_cb_t semihosting_cb; using mem_type = util::sparse_array; using csr_type = util::sparse_array::reg_t, 1ULL << 12, 12>; diff --git a/src/iss/semihosting/semihosting.cpp b/src/iss/semihosting/semihosting.cpp index af7006e..ab2e2c9 100644 --- a/src/iss/semihosting/semihosting.cpp +++ b/src/iss/semihosting/semihosting.cpp @@ -1,55 +1,56 @@ #include "semihosting.h" -#include -#include -#include -#include #include +#include +#include +#include +#include // explanation of syscalls can be found at https://github.com/SpinalHDL/openocd_riscv/blob/riscv_spinal/src/target/semihosting_common.h -const char *SYS_OPEN_MODES_STRS[] = { "r", "rb", "r+", "r+b", "w", "wb", "w+", "w+b", "a", "ab", "a+", "a+b" }; +const char* SYS_OPEN_MODES_STRS[] = {"r", "rb", "r+", "r+b", "w", "wb", "w+", "w+b", "a", "ab", "a+", "a+b"}; -template T sh_read_field(iss::arch_if* arch_if_ptr, T addr, int len=4) { +template T sh_read_field(iss::arch_if* arch_if_ptr, T addr, int len = 4) { uint8_t bytes[4]; auto res = arch_if_ptr->read(iss::address_type::PHYSICAL, iss::access_type::DEBUG_READ, 0, addr, 4, &bytes[0]); - //auto res = arch_if_ptr->read(iss::address_type::PHYSICAL, iss::access_type::DEBUG_READ, 0, *parameter, 1, &character); + // auto res = arch_if_ptr->read(iss::address_type::PHYSICAL, iss::access_type::DEBUG_READ, 0, *parameter, 1, &character); - if(res != iss::Ok){ - return 0; //TODO THROW ERROR - } else return static_cast(bytes[0]) | (static_cast(bytes[1]) << 8) | (static_cast(bytes[2]) << 16) | (static_cast(bytes[3]) << 24); + if(res != iss::Ok) { + return 0; // TODO THROW ERROR + } else + return static_cast(bytes[0]) | (static_cast(bytes[1]) << 8) | (static_cast(bytes[2]) << 16) | + (static_cast(bytes[3]) << 24); } -template std::string sh_read_string(iss::arch_if* arch_if_ptr, T addr, T str_len){ +template std::string sh_read_string(iss::arch_if* arch_if_ptr, T addr, T str_len) { std::vector buffer(str_len); - for (int i = 0; i < str_len; i++ ) { + for(int i = 0; i < str_len; i++) { buffer[i] = sh_read_field(arch_if_ptr, addr + i, 1); } std::string str(buffer.begin(), buffer.end()); return str; } - template void semihosting_callback::operator()(iss::arch_if* arch_if_ptr, T* call_number, T* parameter) { - static std::map openFiles; + static std::map openFiles; static T file_count = 3; static T semihostingErrno; switch(static_cast(*call_number)) { case semihosting_syscalls::SYS_CLOCK: { - auto end = std::chrono::high_resolution_clock::now(); // end measurement + auto end = std::chrono::high_resolution_clock::now(); // end measurement auto elapsed = end - timeVar; auto millis = std::chrono::duration_cast(elapsed).count(); - *call_number = millis; //TODO get time now + *call_number = millis; // TODO get time now break; } case semihosting_syscalls::SYS_CLOSE: { T file_handle = *parameter; - if (openFiles.size() <= file_handle && file_handle < 0) { + if(openFiles.size() <= file_handle && file_handle < 0) { semihostingErrno = EBADF; return; } auto file = openFiles[file_handle]; openFiles.erase(file_handle); - if (!(file == stdin || file == stdout || file == stderr)) { + if(!(file == stdin || file == stdout || file == stderr)) { int i = fclose(file); *call_number = i; } else { @@ -80,7 +81,8 @@ template void semihosting_callback::operator()(iss::arch_if* arc auto file = openFiles[file_handle]; size_t currentPos = ftell(file); - if (currentPos < 0) throw std::runtime_error("SYS_FLEN negative value"); + if(currentPos < 0) + throw std::runtime_error("SYS_FLEN negative value"); fseek(file, 0, SEEK_END); size_t length = ftell(file); fseek(file, currentPos, SEEK_SET); @@ -101,36 +103,36 @@ template void semihosting_callback::operator()(iss::arch_if* arc break; } case semihosting_syscalls::SYS_ISTTY: { - T file_handle = *parameter; + T file_handle = *parameter; *call_number = (file_handle == 0 || file_handle == 1 || file_handle == 2); break; } case semihosting_syscalls::SYS_OPEN: { T path_str_addr = sh_read_field(arch_if_ptr, *parameter); - T mode = sh_read_field(arch_if_ptr, 4+(*parameter)); - T path_len = sh_read_field(arch_if_ptr, 8+(*parameter)); + T mode = sh_read_field(arch_if_ptr, 4 + (*parameter)); + T path_len = sh_read_field(arch_if_ptr, 8 + (*parameter)); std::string path_str = sh_read_string(arch_if_ptr, path_str_addr, path_len); - //TODO LOG INFO + // TODO LOG INFO - if (mode >= 12) { - //TODO throw ERROR + if(mode >= 12) { + // TODO throw ERROR return; } - FILE *file = nullptr; + FILE* file = nullptr; if(path_str == ":tt") { - if (mode < 4) + if(mode < 4) file = stdin; - else if (mode < 8) + else if(mode < 8) file = stdout; - else + else file = stderr; } else { file = fopen(path_str.c_str(), SYS_OPEN_MODES_STRS[mode]); - if (file == nullptr) { - //TODO throw error + if(file == nullptr) { + // TODO throw error return; } } @@ -138,45 +140,41 @@ template void semihosting_callback::operator()(iss::arch_if* arc openFiles[file_handle] = file; *call_number = file_handle; break; - } case semihosting_syscalls::SYS_READ: { - T file_handle = sh_read_field(arch_if_ptr, (*parameter)+4); + T file_handle = sh_read_field(arch_if_ptr, (*parameter) + 4); T addr = sh_read_field(arch_if_ptr, *parameter); - T count = sh_read_field(arch_if_ptr, (*parameter)+8); + T count = sh_read_field(arch_if_ptr, (*parameter) + 8); auto file = openFiles[file_handle]; std::vector buffer(count); size_t num_read = 0; - if (file == stdin) - { + if(file == stdin) { // when reading from stdin: mimic behaviour from read syscall // and return on newline. - while (num_read < count) - { + while(num_read < count) { char c = fgetc(file); buffer[num_read] = c; num_read++; - if (c == '\n') + if(c == '\n') break; } } else { num_read = fread(buffer.data(), 1, count, file); } buffer.resize(num_read); - for(int i = 0; iwrite(iss::address_type::PHYSICAL, iss::access_type::DEBUG_READ, 0, addr+i, 1, &buffer[i]); + for(int i = 0; i < num_read; i++) { + auto res = arch_if_ptr->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_READ, 0, addr + i, 1, &buffer[i]); if(res != iss::Ok) return; } *call_number = count - num_read; break; - } case semihosting_syscalls::SYS_READC: { uint8_t character = getchar(); - //character = getchar(); + // character = getchar(); /*if(character != iss::Ok) std::cout << "Not OK"; return;*/ @@ -185,36 +183,38 @@ template void semihosting_callback::operator()(iss::arch_if* arc } case semihosting_syscalls::SYS_REMOVE: { T path_str_addr = sh_read_field(arch_if_ptr, *parameter); - T path_len = sh_read_field(arch_if_ptr, (*parameter)+4); + T path_len = sh_read_field(arch_if_ptr, (*parameter) + 4); std::string path_str = sh_read_string(arch_if_ptr, path_str_addr, path_len); - if(remove(path_str.c_str())<0) *call_number = -1; + if(remove(path_str.c_str()) < 0) + *call_number = -1; break; } case semihosting_syscalls::SYS_RENAME: { T path_str_addr_old = sh_read_field(arch_if_ptr, *parameter); - T path_len_old = sh_read_field(arch_if_ptr, (*parameter)+4); - T path_str_addr_new = sh_read_field(arch_if_ptr, (*parameter)+8); - T path_len_new = sh_read_field(arch_if_ptr, (*parameter)+12); + T path_len_old = sh_read_field(arch_if_ptr, (*parameter) + 4); + T path_str_addr_new = sh_read_field(arch_if_ptr, (*parameter) + 8); + T path_len_new = sh_read_field(arch_if_ptr, (*parameter) + 12); std::string path_str_old = sh_read_string(arch_if_ptr, path_str_addr_old, path_len_old); - std::string path_str_new = sh_read_string(arch_if_ptr, path_str_addr_new, path_len_new); - rename(path_str_old.c_str(), path_str_new.c_str()); + std::string path_str_new = sh_read_string(arch_if_ptr, path_str_addr_new, path_len_new); + rename(path_str_old.c_str(), path_str_new.c_str()); break; } case semihosting_syscalls::SYS_SEEK: { T file_handle = sh_read_field(arch_if_ptr, *parameter); - T pos = sh_read_field(arch_if_ptr, (*parameter)+1); + T pos = sh_read_field(arch_if_ptr, (*parameter) + 1); auto file = openFiles[file_handle]; int retval = fseek(file, pos, SEEK_SET); - if(retval<0) throw std::runtime_error("SYS_SEEK negative return value"); + if(retval < 0) + throw std::runtime_error("SYS_SEEK negative return value"); break; } case semihosting_syscalls::SYS_SYSTEM: { T cmd_addr = sh_read_field(arch_if_ptr, *parameter); - T cmd_len = sh_read_field(arch_if_ptr, (*parameter)+1); + T cmd_len = sh_read_field(arch_if_ptr, (*parameter) + 1); std::string cmd = sh_read_string(arch_if_ptr, cmd_addr, cmd_len); system(cmd.c_str()); break; @@ -224,16 +224,16 @@ template void semihosting_callback::operator()(iss::arch_if* arc break; } case semihosting_syscalls::SYS_TIME: { - //returns time in seconds scince 01.01.1970 00:00 + // returns time in seconds scince 01.01.1970 00:00 *call_number = time(NULL); break; } case semihosting_syscalls::SYS_TMPNAM: { T buffer_addr = sh_read_field(arch_if_ptr, *parameter); - T identifier = sh_read_field(arch_if_ptr, (*parameter)+1); - T buffer_len = sh_read_field(arch_if_ptr, (*parameter)+2); + T identifier = sh_read_field(arch_if_ptr, (*parameter) + 1); + T buffer_len = sh_read_field(arch_if_ptr, (*parameter) + 2); - if (identifier > 255) { + if(identifier > 255) { *call_number = -1; return; } @@ -243,15 +243,16 @@ template void semihosting_callback::operator()(iss::arch_if* arc for(int i = 0; i < buffer_len; i++) { uint8_t character = filename[i]; - auto res = arch_if_ptr->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_READ, 0, (*parameter)+i, 1, &character); - if(res != iss::Ok) return; + auto res = arch_if_ptr->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_READ, 0, (*parameter) + i, 1, &character); + if(res != iss::Ok) + return; } break; } case semihosting_syscalls::SYS_WRITE: { - T file_handle = sh_read_field(arch_if_ptr, (*parameter)+4); + T file_handle = sh_read_field(arch_if_ptr, (*parameter) + 4); T addr = sh_read_field(arch_if_ptr, *parameter); - T count = sh_read_field(arch_if_ptr, (*parameter)+8); + T count = sh_read_field(arch_if_ptr, (*parameter) + 8); auto file = openFiles[file_handle]; std::string str = sh_read_string(arch_if_ptr, addr, count); diff --git a/src/iss/semihosting/semihosting.h b/src/iss/semihosting/semihosting.h index 295c4d4..1e62df2 100644 --- a/src/iss/semihosting/semihosting.h +++ b/src/iss/semihosting/semihosting.h @@ -1,8 +1,8 @@ #ifndef _SEMIHOSTING_H_ #define _SEMIHOSTING_H_ -#include -#include #include +#include +#include /* * According to: * "Semihosting for AArch32 and AArch64, Release 2.0" @@ -50,9 +50,10 @@ enum class semihosting_syscalls { USER_CMD_0x1FF = 0x1FF, }; -template struct semihosting_callback{ +template struct semihosting_callback { std::chrono::high_resolution_clock::time_point timeVar; - semihosting_callback(): timeVar(std::chrono::high_resolution_clock::now()) {} + semihosting_callback() + : timeVar(std::chrono::high_resolution_clock::now()) {} void operator()(iss::arch_if* arch_if_ptr, T* call_number, T* parameter); }; diff --git a/src/main.cpp b/src/main.cpp index f006ca6..940a106 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -119,8 +119,8 @@ int main(int argc, char* argv[]) { // instantiate the simulator iss::vm_ptr vm{nullptr}; iss::cpu_ptr cpu{nullptr}; - semihosting_callback cb{}; - semihosting_cb_t semihosting_cb = [&cb](iss::arch_if* i, uint32_t* a0, uint32_t* a1) {cb(i,a0,a1);}; + semihosting_callback cb{}; + semihosting_cb_t semihosting_cb = [&cb](iss::arch_if* i, uint32_t* a0, uint32_t* a1) { cb(i, a0, a1); }; std::string isa_opt(clim["isa"].as()); if(isa_opt.size() == 0 || isa_opt == "?") { auto list = f.get_names();