From ce7343075f47acc10a9b29fb1bb4ee815870b774 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 11 Jun 2022 08:05:19 +0200 Subject: [PATCH] moved file-loaders into loaders.cpp/h --- CMakeLists.txt | 1 + loaders.cpp | 205 +++++++++++++++++++++++++++++++++++++++++++++++++ loaders.h | 12 +++ main.cpp | 198 +---------------------------------------------- 4 files changed, 220 insertions(+), 196 deletions(-) create mode 100644 loaders.cpp create mode 100644 loaders.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e20e176..1c854a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable( debugger.cpp error.cpp kw11-l.cpp + loaders.cpp main.cpp memory.cpp rk05.cpp diff --git a/loaders.cpp b/loaders.cpp new file mode 100644 index 0000000..6057364 --- /dev/null +++ b/loaders.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + +#include "bus.h" +#include "cpu.h" +#include "error.h" +#include "gen.h" +#include "loaders.h" +#include "utils.h" + + +void loadbin(bus *const b, uint16_t base, const char *const file) +{ + FILE *fh = fopen(file, "rb"); + + while(!feof(fh)) + b -> writeByte(base++, fgetc(fh)); + + fclose(fh); +} + +void setBootLoader(bus *const b, const bootloader_t which) +{ + cpu *const c = b -> getCpu(); + + uint16_t offset = 0; + const uint16_t *bl = nullptr; + int size = 0; + + if (which == BL_RK05) { + offset = 01000; + + static uint16_t rk05_code[] = { + 0012700, + 0177406, + 0012710, + 0177400, + 0012740, + 0000005, + 0105710, + 0100376, + 0005007 + }; + + bl = rk05_code; + + size = 9; + } + else if (which == BL_RL02) { + offset = 01000; + + /* from https://www.pdp-11.nl/peripherals/disk/rl-info.html + static uint16_t rl02_code[] = { + 0012701, + 0174400, + 0012761, + 0000013, + 0000004, + 0012711, + 0000004, + 0105711, + 0100376, + 0005061, + 0000002, + 0005061, + 0000004, + 0012761, + 0177400, + 0000006, + 0012711, + 0000014, + 0105711, + 0100376, + 0005007 + }; + + size = 21; + */ + + // from http://gunkies.org/wiki/RL11_disk_controller + static uint16_t rl02_code[] = { + 0012700, + 0174400, + 0012760, + 0177400, + 0000006, + 0012710, + 0000014, + 0105710, + 0100376, + 0005007, + }; + + size = 10; + + bl = rl02_code; + } + + for(int i=0; i writeWord(offset + i * 2, bl[i]); + + c -> setRegister(7, offset); +} + +uint16_t loadTape(bus *const b, const char *const file) +{ + FILE *fh = fopen(file, "rb"); + if (!fh) { + fprintf(stderr, "Cannot open %s\n", file); + return -1; + } + + uint16_t start = 0, end = 0; + + for(;!feof(fh);) { + uint8_t buffer[6]; + + if (fread(buffer, 1, 6, fh) != 6) + break; + + int count = (buffer[3] << 8) | buffer[2]; + int p = (buffer[5] << 8) | buffer[4]; + + uint8_t csum = 0; + for(int i=2; i<6; i++) + csum += buffer[i]; + + if (count == 6) { // eg no data + if (p != 1) { + D(fprintf(stderr, "Setting start address to %o\n", p);) + start = p; + } + } + + D(fprintf(stderr, "%ld] reading %d (dec) bytes to %o (oct)\n", ftell(fh), count - 6, p);) + + for(int i=0; i writeByte(p++, c); + + if (p > end) + end = p; + } + + int fcs = fgetc(fh); + csum += fcs; + + if (csum != 255) + fprintf(stderr, "checksum error %d\n", csum); + } + + fclose(fh); + + fh = fopen("test.dat", "wb"); + for(int i=0; i readByte(i), fh); + fclose(fh); + + return start; +} + +void load_p11_x11(bus *const b, const std::string & file) +{ + FILE *fh = fopen(file.c_str(), "rb"); + if (!fh) + error_exit(true, "Cannot open %s", file.c_str()); + + uint16_t addr = 0; + int n = 0; + + while(!feof(fh)) { + char buffer[4096]; + + if (!fgets(buffer, sizeof buffer, fh)) + break; + + if (n) { + uint8_t byte = strtol(buffer, NULL, 16); + + b->writeByte(addr, byte); + + n--; + + addr++; + } + else { + std::vector parts = split(buffer, " "); + + addr = strtol(parts[0].c_str(), NULL, 16); + n = strtol(parts[1].c_str(), NULL, 16); + } + } + + fclose(fh); + + cpu *const c = b -> getCpu(); + c -> setRegister(7, 0); +} diff --git a/loaders.h b/loaders.h new file mode 100644 index 0000000..5825f0c --- /dev/null +++ b/loaders.h @@ -0,0 +1,12 @@ +#include +#include + +#include "bus.h" + + +typedef enum { BL_NONE, BL_RK05, BL_RL02 } bootloader_t; + +void loadbin(bus *const b, uint16_t base, const char *const file); +void setBootLoader(bus *const b, const bootloader_t which); +uint16_t loadTape(bus *const b, const char *const file); +void load_p11_x11(bus *const b, const std::string & file); diff --git a/main.cpp b/main.cpp index 602d8bf..d8f2a39 100644 --- a/main.cpp +++ b/main.cpp @@ -14,6 +14,7 @@ #include "debugger.h" #include "gen.h" #include "kw11-l.h" +#include "loaders.h" #include "memory.h" #include "terminal.h" #include "tests.h" @@ -21,208 +22,13 @@ #include "utils.h" -typedef enum { BL_NONE, BL_RK05, BL_RL02 } bootloader_t; - bool withUI { false }; std::atomic_uint32_t event { 0 }; std::atomic_bool *running { nullptr }; bool trace_output { false }; -void loadbin(bus *const b, uint16_t base, const char *const file) -{ - FILE *fh = fopen(file, "rb"); +std::atomic_bool sw { false }; - while(!feof(fh)) - b -> writeByte(base++, fgetc(fh)); - - fclose(fh); -} - -void setBootLoader(bus *const b, const bootloader_t which) -{ - cpu *const c = b -> getCpu(); - - uint16_t offset = 0; - const uint16_t *bl = nullptr; - int size = 0; - - if (which == BL_RK05) { - offset = 01000; - - static uint16_t rk05_code[] = { - 0012700, - 0177406, - 0012710, - 0177400, - 0012740, - 0000005, - 0105710, - 0100376, - 0005007 - }; - - bl = rk05_code; - - size = 9; - } - else if (which == BL_RL02) { - offset = 01000; - - /* from https://www.pdp-11.nl/peripherals/disk/rl-info.html - static uint16_t rl02_code[] = { - 0012701, - 0174400, - 0012761, - 0000013, - 0000004, - 0012711, - 0000004, - 0105711, - 0100376, - 0005061, - 0000002, - 0005061, - 0000004, - 0012761, - 0177400, - 0000006, - 0012711, - 0000014, - 0105711, - 0100376, - 0005007 - }; - - size = 21; - */ - - // from http://gunkies.org/wiki/RL11_disk_controller - static uint16_t rl02_code[] = { - 0012700, - 0174400, - 0012760, - 0177400, - 0000006, - 0012710, - 0000014, - 0105710, - 0100376, - 0005007, - }; - - size = 10; - - bl = rl02_code; - } - - for(int i=0; i writeWord(offset + i * 2, bl[i]); - - c -> setRegister(7, offset); -} - -uint16_t loadTape(bus *const b, const char *const file) -{ - FILE *fh = fopen(file, "rb"); - if (!fh) { - fprintf(stderr, "Cannot open %s\n", file); - return -1; - } - - uint16_t start = 0, end = 0; - - for(;!feof(fh);) { - uint8_t buffer[6]; - - if (fread(buffer, 1, 6, fh) != 6) - break; - - int count = (buffer[3] << 8) | buffer[2]; - int p = (buffer[5] << 8) | buffer[4]; - - uint8_t csum = 0; - for(int i=2; i<6; i++) - csum += buffer[i]; - - if (count == 6) { // eg no data - if (p != 1) { - D(fprintf(stderr, "Setting start address to %o\n", p);) - start = p; - } - } - - D(fprintf(stderr, "%ld] reading %d (dec) bytes to %o (oct)\n", ftell(fh), count - 6, p);) - - for(int i=0; i writeByte(p++, c); - - if (p > end) - end = p; - } - - int fcs = fgetc(fh); - csum += fcs; - - if (csum != 255) - fprintf(stderr, "checksum error %d\n", csum); - } - - fclose(fh); - - fh = fopen("test.dat", "wb"); - for(int i=0; i readByte(i), fh); - fclose(fh); - - return start; -} - -void load_p11_x11(bus *const b, const std::string & file) -{ - FILE *fh = fopen(file.c_str(), "rb"); - if (!fh) - error_exit(true, "Cannot open %s", file.c_str()); - - uint16_t addr = 0; - int n = 0; - - while(!feof(fh)) { - char buffer[4096]; - - if (!fgets(buffer, sizeof buffer, fh)) - break; - - if (n) { - uint8_t byte = strtol(buffer, NULL, 16); - - b->writeByte(addr, byte); - - n--; - - addr++; - } - else { - std::vector parts = split(buffer, " "); - - addr = strtol(parts[0].c_str(), NULL, 16); - n = strtol(parts[1].c_str(), NULL, 16); - } - } - - fclose(fh); - - cpu *const c = b -> getCpu(); - c -> setRegister(7, 0); -} - -volatile bool sw = false; void sw_handler(int s) { if (s == SIGWINCH)