Skip to content

Commit

Permalink
refactor: isolate OS functions in a dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Nov 9, 2023
1 parent b85a699 commit 80099cc
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 340 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ LIBCARTESI_OBJS:= \
clint.o \
clint-factory.o \
dtb.o \
tty.o \
os.o \
htif.o \
htif-factory.o \
shadow-state.o \
Expand Down
6 changes: 3 additions & 3 deletions src/htif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#include "htif.h"
#include "i-device-state-access.h"
#include "machine-runtime-config.h"
#include "os.h"
#include "pma-constants.h"
#include "strict-aliasing.h"
#include "tty.h"

namespace cartesi {

Expand Down Expand Up @@ -101,15 +101,15 @@ static execute_status htif_console(htif_runtime_config *runtime_config, i_device
// In microarchitecture runtime_config will always be nullptr,
// therefore the HTIF runtime config is actually ignored.
if (!runtime_config || !runtime_config->no_console_putchar) {
tty_putchar(ch);
os_putchar(ch);
}
a->write_htif_fromhost(HTIF_BUILD(HTIF_DEVICE_CONSOLE, cmd, 0));
} else if (cmd == HTIF_CONSOLE_GETCHAR) {
// In blockchain, this command will never be enabled as there is no way to input the same character
// to every participant in a dispute: where would c come from? So if the code reached here in the
// blockchain, there must be some serious bug
// In interactive mode, we just get the next character from the console and send it back in the ack
const int c = tty_getchar();
const int c = os_getchar();
a->write_htif_fromhost(HTIF_BUILD(HTIF_DEVICE_CONSOLE, cmd, c));
}
}
Expand Down
13 changes: 3 additions & 10 deletions src/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@
#include "uarch-step.h"
#include "unique-c-ptr.h"

#ifdef _WIN32
#include <direct.h> // mkdir
#define mkdir(a, b) _mkdir(a)
#else
#include <sys/stat.h> // mkdir
#endif

/// \file
/// \brief Cartesi machine implementation

Expand Down Expand Up @@ -480,7 +473,7 @@ machine::machine(const machine_config &c, const machine_runtime_config &r) :

// Initialize TTY if console input is enabled
if (m_c.htif.console_getchar) {
tty_initialize();
os_open_tty();
}

// Initialize memory range descriptions returned by get_memory_ranges method
Expand Down Expand Up @@ -708,7 +701,7 @@ static void store_hash(const machine::hash_type &h, const std::string &dir) {
}

void machine::store(const std::string &dir) const {
if (mkdir(dir.c_str(), 0700)) {
if (os_mkdir(dir.c_str(), 0700)) {
throw std::runtime_error{"error creating directory '" + dir + "'"};
}
if (!update_merkle_tree()) {
Expand All @@ -726,7 +719,7 @@ void machine::store(const std::string &dir) const {
machine::~machine() {
// Cleanup TTY if console input was enabled
if (m_c.htif.console_getchar) {
tty_finalize();
os_close_tty();
}
#ifdef DUMP_HIST
(void) fprintf(stderr, "\nInstruction Histogram:\n");
Expand Down
Loading

0 comments on commit 80099cc

Please sign in to comment.