From cb66c7e2dcce41483ee6668e726aff4a1db92084 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 8 May 2017 21:05:35 -0400 Subject: [PATCH 1/2] Performed some minor tidying. --- Machines/Commodore/Vic-20/Vic20.cpp | 39 +++++------------------------ 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/Machines/Commodore/Vic-20/Vic20.cpp b/Machines/Commodore/Vic-20/Vic20.cpp index 84d8bd061..04b472e77 100644 --- a/Machines/Commodore/Vic-20/Vic20.cpp +++ b/Machines/Commodore/Vic-20/Vic20.cpp @@ -18,12 +18,12 @@ using namespace Commodore::Vic20; Machine::Machine() : rom_(nullptr), is_running_at_zero_cost_(false), - tape_(new Storage::Tape::BinaryTapePlayer(1022727)) { - // create 6522s, serial port and bus - user_port_via_.reset(new UserPortVIA); - keyboard_via_.reset(new KeyboardVIA); - serial_port_.reset(new SerialPort); - serial_bus_.reset(new ::Commodore::Serial::Bus); + tape_(new Storage::Tape::BinaryTapePlayer(1022727)), + user_port_via_(new UserPortVIA), + keyboard_via_(new KeyboardVIA), + serial_port_(new SerialPort), + serial_bus_(new ::Commodore::Serial::Bus) { + // communicate the tape to the user-port VIA user_port_via_->set_tape(tape_); // wire up the serial bus and serial port @@ -98,17 +98,6 @@ Machine::~Machine() { } unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uint16_t address, uint8_t *value) { -// static int logCount = 0; -// if(operation == CPU6502::BusOperation::ReadOpcode && address == 0xf957) logCount = 500; -// if(operation == CPU6502::BusOperation::ReadOpcode && logCount) { -// logCount--; -// printf("%04x\n", address); -// } - -// if(operation == CPU6502::BusOperation::Write && (address >= 0x033C && address < 0x033C + 192)) { -// printf("\n[%04x] <- %02x\n", address, *value); -// } - // run the phase-1 part of this cycle, in which the VIC accesses memory if(!is_running_at_zero_cost_) mos6560_->run_for_cycles(1); @@ -266,22 +255,6 @@ void Machine::set_rom(ROMSlot slot, size_t length, const uint8_t *data) { } } -//void Machine::set_prg(const char *file_name, size_t length, const uint8_t *data) { -// if(length > 2) { -// _rom_address = (uint16_t)(data[0] | (data[1] << 8)); -// _rom_length = (uint16_t)(length - 2); -// -// // install in the ROM area if this looks like a ROM; otherwise put on tape and throw into that mechanism -// if(_rom_address == 0xa000) { -// _rom = new uint8_t[0x2000]; -// memcpy(_rom, &data[2], length - 2); -// write_to_map(processor_read_memory_map_, _rom, _rom_address, 0x2000); -// } else { -// set_tape(std::shared_ptr(new Storage::Tape::PRG(file_name))); -// } -// } -//} - #pragma mar - Tape void Machine::configure_as_target(const StaticAnalyser::Target &target) { From 5d91a2600db183f7b20b254d2fdc88e9f8c43a46 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 8 May 2017 22:15:35 -0400 Subject: [PATCH 2/2] Permitted ROM-style PRGs that are not a power-of-two in size, and added extra safety checks on loading data from a tape. --- Machines/Commodore/Vic-20/Vic20.cpp | 7 +++++-- Storage/Cartridge/Formats/PRG.cpp | 12 +++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Machines/Commodore/Vic-20/Vic20.cpp b/Machines/Commodore/Vic-20/Vic20.cpp index 04b472e77..3d48552f2 100644 --- a/Machines/Commodore/Vic-20/Vic20.cpp +++ b/Machines/Commodore/Vic-20/Vic20.cpp @@ -147,10 +147,13 @@ unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uin // perform a via-processor_write_memory_map_ memcpy uint8_t *data_ptr = data->data.data(); - while(start_address != end_address) { - processor_write_memory_map_[start_address >> 10][start_address & 0x3ff] = *data_ptr; + size_t data_left = data->data.size(); + while(data_left && start_address != end_address) { + uint8_t *page = processor_write_memory_map_[start_address >> 10]; + if(page) page[start_address & 0x3ff] = *data_ptr; data_ptr++; start_address++; + data_left--; } // set tape status, carry and flag diff --git a/Storage/Cartridge/Formats/PRG.cpp b/Storage/Cartridge/Formats/PRG.cpp index 704487c1c..923ed7ac4 100644 --- a/Storage/Cartridge/Formats/PRG.cpp +++ b/Storage/Cartridge/Formats/PRG.cpp @@ -18,12 +18,8 @@ PRG::PRG(const char *file_name) { struct stat file_stats; stat(file_name, &file_stats); - // accept only files sized 1, 2, 4 or 8kb - if( - file_stats.st_size != 0x400 + 2 && - file_stats.st_size != 0x800 + 2 && - file_stats.st_size != 0x1000 + 2 && - file_stats.st_size != 0x2000 + 2) + // accept only files sized less than 8kb + if(file_stats.st_size > 0x2000 + 2) throw ErrorNotROM; // get the loading address, and the rest of the contents @@ -33,7 +29,9 @@ PRG::PRG(const char *file_name) { loading_address |= fgetc(file) << 8; size_t data_length = (size_t)file_stats.st_size - 2; - std::vector contents(data_length); + size_t padded_data_length = 1; + while(padded_data_length < data_length) padded_data_length <<= 1; + std::vector contents(padded_data_length); fread(&contents[0], 1, (size_t)(data_length), file); fclose(file);