2016-08-27 22:17:40 +00:00
|
|
|
//
|
|
|
|
// PRG.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 27/08/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-08-27 22:17:40 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "PRG.hpp"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <sys/stat.h>
|
2017-11-12 22:46:06 +00:00
|
|
|
|
2016-09-29 23:39:13 +00:00
|
|
|
#include "../Encodings/CommodoreROM.hpp"
|
2016-08-27 22:17:40 +00:00
|
|
|
|
2016-08-27 22:26:51 +00:00
|
|
|
using namespace Storage::Cartridge;
|
2016-08-27 22:17:40 +00:00
|
|
|
|
2018-04-06 21:42:24 +00:00
|
|
|
PRG::PRG(const std::string &file_name) {
|
2016-08-27 22:17:40 +00:00
|
|
|
struct stat file_stats;
|
2018-04-06 21:42:24 +00:00
|
|
|
stat(file_name.c_str(), &file_stats);
|
2016-08-27 22:17:40 +00:00
|
|
|
|
2017-05-09 02:15:35 +00:00
|
|
|
// accept only files sized less than 8kb
|
|
|
|
if(file_stats.st_size > 0x2000 + 2)
|
2016-08-27 22:17:40 +00:00
|
|
|
throw ErrorNotROM;
|
|
|
|
|
|
|
|
// get the loading address, and the rest of the contents
|
2018-04-06 21:42:24 +00:00
|
|
|
FILE *file = std::fopen(file_name.c_str(), "rb");
|
2016-08-27 22:17:40 +00:00
|
|
|
|
|
|
|
int loading_address = fgetc(file);
|
|
|
|
loading_address |= fgetc(file) << 8;
|
|
|
|
|
2020-05-10 03:00:39 +00:00
|
|
|
std::size_t data_length = size_t(file_stats.st_size) - 2;
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t padded_data_length = 1;
|
2017-05-09 02:15:35 +00:00
|
|
|
while(padded_data_length < data_length) padded_data_length <<= 1;
|
|
|
|
std::vector<uint8_t> contents(padded_data_length);
|
2020-05-10 03:00:39 +00:00
|
|
|
std::size_t length = std::fread(contents.data(), 1, size_t(data_length), file);
|
2017-11-12 22:46:06 +00:00
|
|
|
std::fclose(file);
|
2016-08-27 22:17:40 +00:00
|
|
|
|
|
|
|
// accept only files intended to load at 0xa000
|
2020-05-10 03:00:39 +00:00
|
|
|
if(loading_address != 0xa000 || length != size_t(data_length))
|
2016-08-27 22:17:40 +00:00
|
|
|
throw ErrorNotROM;
|
|
|
|
|
|
|
|
// also accept only cartridges with the proper signature
|
2016-09-29 23:39:13 +00:00
|
|
|
if(!Storage::Cartridge::Encodings::CommodoreROM::isROM(contents))
|
2016-08-27 22:17:40 +00:00
|
|
|
throw ErrorNotROM;
|
|
|
|
|
2016-12-03 16:59:28 +00:00
|
|
|
segments_.emplace_back(0xa000, 0xa000 + data_length, std::move(contents));
|
2016-08-27 22:17:40 +00:00
|
|
|
}
|