Skip to content

Commit

Permalink
dir/ls
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed May 9, 2024
1 parent cd70bc9 commit 722cdc9
Showing 1 changed file with 85 additions and 58 deletions.
143 changes: 85 additions & 58 deletions debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,55 +78,94 @@ std::optional<disk_backend *> select_nbd_server(console *const cnsl)
}
#endif

std::optional<std::string> select_host_file(console *const c)
void start_disk(console *const cnsl)
{
for(;;) {
static bool disk_started = false;
if (disk_started)
return;

#if defined(ESP32)
cnsl->put_string_lf(format("MISO: %d", int(MISO)));
cnsl->put_string_lf(format("MOSI: %d", int(MOSI)));
cnsl->put_string_lf(format("SCK : %d", int(SCK )));
cnsl->put_string_lf(format("SS : %d", int(SS )));

#endif

#if defined(SHA2017)
if (SD.begin(21, SD_SCK_MHZ(10)))
disk_started = true;
else
SD.initErrorHalt();
#elif !defined(BUILD_FOR_RP2040)
if (SD.begin(SS, SD_SCK_MHZ(15)))
disk_started = true;
else {
auto err = SD.sdErrorCode();
if (err)
cnsl->put_string_lf(format("SDerror: 0x%x, data: 0x%x", err, SD.sdErrorData()));
else
cnsl->put_string_lf("Failed to initialize SD card");
}
#endif
}

void ls_l(console *const cnsl)
{
start_disk(cnsl);

#if defined(linux)
DIR *dir = opendir(".");
if (!dir) {
c->put_string_lf("Cannot access directory");
return { };
}
DIR *dir = opendir(".");
if (!dir) {
cnsl->put_string_lf("Cannot access directory");
return { };
}

dirent *dr = nullptr;
while((dr = readdir(dir))) {
struct stat st { };
dirent *dr = nullptr;
while((dr = readdir(dir))) {
struct stat st { };

if (stat(dr->d_name, &st) == 0)
c->put_string_lf(format("%s\t\t%ld", dr->d_name, st.st_size));
}
if (stat(dr->d_name, &st) == 0)
cnsl->put_string_lf(format("%s\t\t%ld", dr->d_name, st.st_size));
}

closedir(dir);
closedir(dir);
#elif defined(BUILD_FOR_RP2040)
File root = SD.open("/");
File root = SD.open("/");

for(;;) {
auto entry = root.openNextFile();
if (!entry)
break;

if (!entry.isDirectory()) {
c->put_string(entry.name());
c->put_string("\t\t");
c->put_string_lf(format("%ld", entry.size()));
}
for(;;) {
auto entry = root.openNextFile();
if (!entry)
break;

entry.close();
if (!entry.isDirectory()) {
cnsl->put_string(entry.name());
cnsl->put_string("\t\t");
cnsl->put_string_lf(format("%ld", entry.size()));
}

entry.close();
}
#elif defined(_WIN32)
#else
SD.ls("/", LS_DATE | LS_SIZE | LS_R);
SD.ls("/", LS_DATE | LS_SIZE | LS_R);
#endif
}

std::optional<std::string> select_host_file(console *const cnsl)
{
for(;;) {
ls_l(cnsl);

c->flush_input();
cnsl->flush_input();

std::string selected_file = c->read_line("Enter filename (or empty to abort): ");
std::string selected_file = cnsl->read_line("Enter filename (or empty to abort): ");

if (selected_file.empty())
return { };

c->put_string("Opening file: ");
c->put_string_lf(selected_file.c_str());
cnsl->put_string("Opening file: ");
cnsl->put_string_lf(selected_file.c_str());

bool can_open_file = false;

Expand All @@ -143,41 +182,23 @@ std::optional<std::string> select_host_file(console *const c)
if (can_open_file)
return selected_file;

c->put_string_lf("open failed");
cnsl->put_string_lf("open failed");
}
}

// disk image files
std::optional<disk_backend *> select_disk_file(console *const c)
std::optional<disk_backend *> select_disk_file(console *const cnsl)
{
start_disk(cnsl);

#if IS_POSIX || defined(_WIN32)
c->put_string_lf("Files in current directory: ");
cnsl->put_string_lf("Files in current directory: ");
#else
c->put_string_lf(format("MISO: %d", int(MISO)));
c->put_string_lf(format("MOSI: %d", int(MOSI)));
c->put_string_lf(format("SCK : %d", int(SCK )));
c->put_string_lf(format("SS : %d", int(SS )));

c->put_string_lf("Files on SD-card:");

#if defined(SHA2017)
if (!SD.begin(21, SD_SCK_MHZ(10)))
SD.initErrorHalt();
#elif !defined(BUILD_FOR_RP2040)
if (!SD.begin(SS, SD_SCK_MHZ(15))) {
auto err = SD.sdErrorCode();
if (err)
c->put_string_lf(format("SDerror: 0x%x, data: 0x%x", err, SD.sdErrorData()));
else
c->put_string_lf("Failed to initialize SD card");

return { };
}
#endif
cnsl->put_string_lf("Files on SD-card:");
#endif

for(;;) {
auto selected_file = select_host_file(c);
auto selected_file = select_host_file(cnsl);

if (selected_file.has_value() == false)
break;
Expand All @@ -189,8 +210,8 @@ std::optional<disk_backend *> select_disk_file(console *const c)
#endif

if (!temp->begin(false)) {
c->put_string("Cannot use: ");
c->put_string_lf(selected_file.value().c_str());
cnsl->put_string("Cannot use: ");
cnsl->put_string_lf(selected_file.value().c_str());

delete temp;

Expand Down Expand Up @@ -1008,6 +1029,11 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto

continue;
}
else if (cmd == "dir" || cmd == "ls") {
ls_l(cnsl);

continue;
}
else if (cmd == "ult") {
tm11_unload_tape(b);

Expand Down Expand Up @@ -1066,6 +1092,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
"setmem - set memory (a=) to value (v=), both in octal, one byte",
"toggle - set switch (s=, 0...15 (decimal)) of the front panel to state (t=, 0 or 1)",
"cls - clear screen",
"dir - list files",
"bic - run BIC/LDA file",
"lt - load tape (parameter is filename)",
"ult - unload tape",
Expand Down

0 comments on commit 722cdc9

Please sign in to comment.