2017-11-25 18:18:24 +00:00
|
|
|
//
|
|
|
|
// StaticAnalyser.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 25/11/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-11-25 18:18:24 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "StaticAnalyser.hpp"
|
|
|
|
|
2018-01-26 03:16:46 +00:00
|
|
|
#include "Cartridge.hpp"
|
2017-12-29 20:11:10 +00:00
|
|
|
#include "Tape.hpp"
|
2018-04-03 02:42:41 +00:00
|
|
|
#include "Target.hpp"
|
|
|
|
|
2018-01-01 02:23:30 +00:00
|
|
|
#include "../Disassembler/Z80.hpp"
|
|
|
|
#include "../Disassembler/AddressMapper.hpp"
|
2017-12-29 20:11:10 +00:00
|
|
|
|
2018-01-01 02:36:24 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-01-26 03:16:46 +00:00
|
|
|
static std::unique_ptr<Analyser::Static::Target> CartridgeTarget(
|
|
|
|
const Storage::Cartridge::Cartridge::Segment &segment,
|
|
|
|
uint16_t start_address,
|
|
|
|
Analyser::Static::MSX::Cartridge::Type type,
|
|
|
|
float confidence) {
|
|
|
|
|
|
|
|
// Size down to a multiple of 8kb in size and apply the start address.
|
|
|
|
std::vector<Storage::Cartridge::Cartridge::Segment> output_segments;
|
|
|
|
if(segment.data.size() & 0x1fff) {
|
|
|
|
std::vector<uint8_t> truncated_data;
|
2024-11-30 02:08:35 +00:00
|
|
|
const auto truncated_size =
|
|
|
|
std::vector<uint8_t>::difference_type(segment.data.size()) & ~0x1fff;
|
2018-01-26 03:16:46 +00:00
|
|
|
truncated_data.insert(truncated_data.begin(), segment.data.begin(), segment.data.begin() + truncated_size);
|
|
|
|
output_segments.emplace_back(start_address, truncated_data);
|
|
|
|
} else {
|
|
|
|
output_segments.emplace_back(start_address, segment.data);
|
|
|
|
}
|
|
|
|
|
2019-12-22 04:52:04 +00:00
|
|
|
auto target = std::make_unique<Analyser::Static::MSX::Target>();
|
2018-01-26 03:16:46 +00:00
|
|
|
target->confidence = confidence;
|
|
|
|
|
2023-03-13 03:14:24 +00:00
|
|
|
// Observation: all ROMs of 48kb or less are from the MSX 1 era.
|
|
|
|
if(segment.data.size() < 48*1024) {
|
|
|
|
target->model = Analyser::Static::MSX::Target::Model::MSX1;
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:16:46 +00:00
|
|
|
if(type == Analyser::Static::MSX::Cartridge::Type::None) {
|
|
|
|
target->media.cartridges.emplace_back(new Storage::Cartridge::Cartridge(output_segments));
|
|
|
|
} else {
|
|
|
|
target->media.cartridges.emplace_back(new Analyser::Static::MSX::Cartridge(output_segments, type));
|
|
|
|
}
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2017-12-02 21:01:30 +00:00
|
|
|
/*
|
2017-12-29 20:11:10 +00:00
|
|
|
Expected standard cartridge format:
|
|
|
|
|
|
|
|
DEFB "AB" ; expansion ROM header
|
|
|
|
DEFW initcode ; start of the init code, 0 if no initcode
|
|
|
|
DEFW callstat; pointer to CALL statement handler, 0 if no such handler
|
|
|
|
DEFW device; pointer to expansion device handler, 0 if no such handler
|
|
|
|
DEFW basic ; pointer to the start of a tokenized basicprogram, 0 if no basicprogram
|
|
|
|
DEFS 6,0 ; room reserved for future extensions
|
2018-01-26 03:16:46 +00:00
|
|
|
|
|
|
|
MSX cartridges often include banking hardware; those games were marketed as MegaROMs. The file
|
|
|
|
format that the MSX community has decided upon doesn't retain the type of hardware included, so
|
|
|
|
this analyser has to guess.
|
|
|
|
|
|
|
|
(additional audio hardware is also sometimes included, but it's implied by the banking hardware)
|
2017-12-02 21:01:30 +00:00
|
|
|
*/
|
2018-04-14 23:46:38 +00:00
|
|
|
static Analyser::Static::TargetList CartridgeTargetsFrom(
|
2018-01-26 03:16:46 +00:00
|
|
|
const std::vector<std::shared_ptr<Storage::Cartridge::Cartridge>> &cartridges) {
|
|
|
|
// No cartridges implies no targets.
|
|
|
|
if(cartridges.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
2017-12-02 21:01:30 +00:00
|
|
|
|
2018-04-14 23:46:38 +00:00
|
|
|
Analyser::Static::TargetList targets;
|
2017-12-02 21:01:30 +00:00
|
|
|
for(const auto &cartridge : cartridges) {
|
|
|
|
const auto &segments = cartridge->get_segments();
|
|
|
|
|
|
|
|
// Only one mapped item is allowed.
|
|
|
|
if(segments.size() != 1) continue;
|
|
|
|
|
2018-01-17 03:27:41 +00:00
|
|
|
// Which must be no more than 63 bytes larger than a multiple of 8 kb in size.
|
2024-11-30 02:08:35 +00:00
|
|
|
const Storage::Cartridge::Cartridge::Segment &segment = segments.front();
|
2017-12-02 21:01:30 +00:00
|
|
|
const size_t data_size = segment.data.size();
|
2018-01-17 03:27:41 +00:00
|
|
|
if(data_size < 0x2000 || (data_size & 0x1fff) > 64) continue;
|
2017-12-02 21:01:30 +00:00
|
|
|
|
2018-01-01 16:25:27 +00:00
|
|
|
// Check for a ROM header at address 0; if it's not found then try 0x4000
|
|
|
|
// and adjust the start address;
|
|
|
|
uint16_t start_address = 0;
|
|
|
|
bool found_start = false;
|
|
|
|
if(segment.data[0] == 0x41 && segment.data[1] == 0x42) {
|
|
|
|
start_address = 0x4000;
|
|
|
|
found_start = true;
|
|
|
|
} else if(segment.data.size() >= 0x8000 && segment.data[0x4000] == 0x41 && segment.data[0x4001] == 0x42) {
|
|
|
|
start_address = 0;
|
|
|
|
found_start = true;
|
|
|
|
}
|
2017-12-02 21:01:30 +00:00
|
|
|
|
2018-01-01 16:25:27 +00:00
|
|
|
// Reject cartridge if the ROM header wasn't found.
|
|
|
|
if(!found_start) continue;
|
2017-12-02 21:01:30 +00:00
|
|
|
|
2024-11-30 02:08:35 +00:00
|
|
|
const uint16_t init_address = uint16_t(segment.data[2] | (segment.data[3] << 8));
|
2018-01-01 02:23:30 +00:00
|
|
|
// TODO: check for a rational init address?
|
|
|
|
|
2018-01-26 03:16:46 +00:00
|
|
|
// If this ROM is less than 48kb in size then it's an ordinary ROM. Just emplace it and move on.
|
2023-03-13 03:14:24 +00:00
|
|
|
// Bonus observation: all such ROMs are from the MSX 1 era.
|
2018-01-26 03:16:46 +00:00
|
|
|
if(data_size <= 0xc000) {
|
|
|
|
targets.emplace_back(CartridgeTarget(segment, start_address, Analyser::Static::MSX::Cartridge::Type::None, 1.0));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-01 16:25:27 +00:00
|
|
|
// If this ROM is greater than 48kb in size then some sort of MegaROM scheme must
|
2018-01-01 02:23:30 +00:00
|
|
|
// be at play; disassemble to try to figure it out.
|
2018-01-26 03:16:46 +00:00
|
|
|
std::vector<uint8_t> first_8k;
|
|
|
|
first_8k.insert(first_8k.begin(), segment.data.begin(), segment.data.begin() + 8192);
|
2023-09-07 15:09:33 +00:00
|
|
|
const Analyser::Static::Z80::Disassembly disassembly =
|
2018-01-26 03:16:46 +00:00
|
|
|
Analyser::Static::Z80::Disassemble(
|
|
|
|
first_8k,
|
|
|
|
Analyser::Static::Disassembler::OffsetMapper(start_address),
|
2023-09-04 19:13:06 +00:00
|
|
|
{ init_address },
|
|
|
|
Analyser::Static::Z80::Approach::Exhaustive
|
2018-01-26 03:16:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
using Instruction = Analyser::Static::Z80::Instruction;
|
2023-09-07 02:40:39 +00:00
|
|
|
const std::map<uint16_t, Instruction> &instructions = disassembly.instructions_by_address;
|
2018-01-01 02:23:30 +00:00
|
|
|
|
2018-01-26 03:16:46 +00:00
|
|
|
// Look for LD (nnnn), A instructions, and collate those addresses.
|
|
|
|
std::map<uint16_t, int> address_counts;
|
|
|
|
for(const auto &instruction_pair : instructions) {
|
|
|
|
if( instruction_pair.second.operation == Instruction::Operation::LD &&
|
|
|
|
instruction_pair.second.destination == Instruction::Location::Operand_Indirect &&
|
|
|
|
instruction_pair.second.source == Instruction::Location::A) {
|
2020-05-10 03:00:39 +00:00
|
|
|
address_counts[uint16_t(instruction_pair.second.operand)]++;
|
2018-01-03 03:18:10 +00:00
|
|
|
}
|
2018-01-01 02:23:30 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 02:40:39 +00:00
|
|
|
// Weight confidences by number of observed hits; if any is above 60% confidence, just use it.
|
2024-11-30 02:08:35 +00:00
|
|
|
const auto ascii_8kb_total =
|
|
|
|
address_counts[0x6000] + address_counts[0x6800] + address_counts[0x7000] + address_counts[0x7800];
|
2023-09-07 02:40:39 +00:00
|
|
|
const auto ascii_16kb_total = address_counts[0x6000] + address_counts[0x7000] + address_counts[0x77ff];
|
|
|
|
const auto konami_total = address_counts[0x6000] + address_counts[0x8000] + address_counts[0xa000];
|
2024-11-30 02:08:35 +00:00
|
|
|
const auto konami_with_scc_total =
|
|
|
|
address_counts[0x5000] + address_counts[0x7000] + address_counts[0x9000] + address_counts[0xb000];
|
2023-09-07 02:40:39 +00:00
|
|
|
|
|
|
|
const auto total_hits = ascii_8kb_total + ascii_16kb_total + konami_total + konami_with_scc_total;
|
2018-01-26 03:16:46 +00:00
|
|
|
|
2023-09-07 02:40:39 +00:00
|
|
|
const bool is_ascii_8kb = (ascii_8kb_total * 5) / (total_hits * 3);
|
|
|
|
const bool is_ascii_16kb = (ascii_16kb_total * 5) / (total_hits * 3);
|
|
|
|
const bool is_konami = (konami_total * 5) / (total_hits * 3);
|
|
|
|
const bool is_konami_with_scc = (konami_with_scc_total * 5) / (total_hits * 3);
|
|
|
|
|
|
|
|
if(!is_ascii_16kb && !is_konami && !is_konami_with_scc) {
|
|
|
|
targets.push_back(CartridgeTarget(
|
|
|
|
segment,
|
|
|
|
start_address,
|
|
|
|
Analyser::Static::MSX::Cartridge::ASCII8kb,
|
|
|
|
float(ascii_8kb_total) / float(total_hits)));
|
|
|
|
}
|
|
|
|
if(!is_ascii_8kb && !is_konami && !is_konami_with_scc) {
|
|
|
|
targets.push_back(CartridgeTarget(
|
|
|
|
segment,
|
|
|
|
start_address,
|
|
|
|
Analyser::Static::MSX::Cartridge::ASCII16kb,
|
|
|
|
float(ascii_16kb_total) / float(total_hits)));
|
|
|
|
}
|
|
|
|
if(!is_ascii_8kb && !is_ascii_16kb && !is_konami_with_scc) {
|
2018-01-26 03:16:46 +00:00
|
|
|
targets.push_back(CartridgeTarget(
|
|
|
|
segment,
|
|
|
|
start_address,
|
|
|
|
Analyser::Static::MSX::Cartridge::Konami,
|
2023-09-07 02:40:39 +00:00
|
|
|
float(konami_total) / float(total_hits)));
|
2018-01-26 03:16:46 +00:00
|
|
|
}
|
2023-09-07 02:40:39 +00:00
|
|
|
if(!is_ascii_8kb && !is_ascii_16kb && !is_konami) {
|
2018-01-26 03:16:46 +00:00
|
|
|
targets.push_back(CartridgeTarget(
|
|
|
|
segment,
|
|
|
|
start_address,
|
|
|
|
Analyser::Static::MSX::Cartridge::KonamiWithSCC,
|
2023-09-07 02:40:39 +00:00
|
|
|
float(konami_with_scc_total) / float(total_hits)));
|
2018-01-17 03:27:41 +00:00
|
|
|
}
|
2017-12-02 21:01:30 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 03:16:46 +00:00
|
|
|
return targets;
|
2017-12-02 21:01:30 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 02:08:35 +00:00
|
|
|
Analyser::Static::TargetList Analyser::Static::MSX::GetTargets(
|
|
|
|
const Media &media,
|
|
|
|
const std::string &,
|
|
|
|
TargetPlatform::IntType
|
|
|
|
) {
|
2018-04-14 16:12:12 +00:00
|
|
|
TargetList destination;
|
|
|
|
|
2018-01-26 03:16:46 +00:00
|
|
|
// Append targets for any cartridges that look correct.
|
2018-04-03 02:42:41 +00:00
|
|
|
auto cartridge_targets = CartridgeTargetsFrom(media.cartridges);
|
2018-01-26 03:16:46 +00:00
|
|
|
std::move(cartridge_targets.begin(), cartridge_targets.end(), std::back_inserter(destination));
|
2017-12-02 21:01:30 +00:00
|
|
|
|
2018-01-26 03:16:46 +00:00
|
|
|
// Consider building a target for disks and/or tapes.
|
2019-12-22 04:52:04 +00:00
|
|
|
auto target = std::make_unique<Target>();
|
2017-12-02 21:01:30 +00:00
|
|
|
|
2017-12-29 20:11:10 +00:00
|
|
|
// Check tapes for loadable files.
|
2018-05-01 02:23:57 +00:00
|
|
|
for(auto &tape : media.tapes) {
|
2024-11-30 02:08:35 +00:00
|
|
|
const std::vector<File> files_on_tape = GetFiles(tape);
|
2017-12-29 20:11:10 +00:00
|
|
|
if(!files_on_tape.empty()) {
|
|
|
|
switch(files_on_tape.front().type) {
|
2018-01-25 03:35:54 +00:00
|
|
|
case File::Type::ASCII: target->loading_command = "RUN\"CAS:\r"; break;
|
|
|
|
case File::Type::TokenisedBASIC: target->loading_command = "CLOAD\rRUN\r"; break;
|
|
|
|
case File::Type::Binary: target->loading_command = "BLOAD\"CAS:\",R\r"; break;
|
2017-12-29 20:11:10 +00:00
|
|
|
default: break;
|
|
|
|
}
|
2018-01-25 03:35:54 +00:00
|
|
|
target->media.tapes.push_back(tape);
|
2017-12-29 20:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-20 02:53:04 +00:00
|
|
|
|
2019-03-01 01:59:07 +00:00
|
|
|
// Region selection: for now, this as simple as:
|
|
|
|
// "If a tape is involved, be European. Otherwise be American (i.e. English, but 60Hz)".
|
|
|
|
target->region = target->media.tapes.empty() ? Target::Region::USA : Target::Region::Europe;
|
|
|
|
|
2018-01-07 21:35:57 +00:00
|
|
|
// Blindly accept disks for now.
|
2019-06-05 01:41:09 +00:00
|
|
|
// TODO: how to spot an MSX disk?
|
2018-01-25 03:35:54 +00:00
|
|
|
target->media.disks = media.disks;
|
2018-04-03 02:42:41 +00:00
|
|
|
target->has_disk_drive = !media.disks.empty();
|
2018-01-07 21:35:57 +00:00
|
|
|
|
2018-01-25 03:35:54 +00:00
|
|
|
if(!target->media.empty()) {
|
2018-03-07 19:24:52 +00:00
|
|
|
target->confidence = 0.5;
|
2018-01-25 03:35:54 +00:00
|
|
|
destination.push_back(std::move(target));
|
2017-12-02 21:01:30 +00:00
|
|
|
}
|
2018-04-14 16:12:12 +00:00
|
|
|
|
|
|
|
return destination;
|
2017-11-25 18:18:24 +00:00
|
|
|
}
|