2022-06-18 17:28:15 +00:00
|
|
|
//
|
|
|
|
// 65816ComparativeTests.m
|
|
|
|
// Clock SignalTests
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/06/2022.
|
|
|
|
// Copyright © 2022 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "65816.hpp"
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
#include <array>
|
2022-06-22 01:47:18 +00:00
|
|
|
#include <optional>
|
2022-06-18 17:28:15 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-06-18 20:26:40 +00:00
|
|
|
struct StopException {};
|
|
|
|
|
2022-06-18 17:28:15 +00:00
|
|
|
struct BusHandler: public CPU::MOS6502Esque::BusHandler<uint32_t> {
|
|
|
|
// Use a map to store RAM contents, in order to preserve initialised state.
|
|
|
|
std::unordered_map<uint32_t, uint8_t> ram;
|
2022-06-18 20:26:40 +00:00
|
|
|
std::unordered_map<uint32_t, uint8_t> inventions;
|
2022-06-18 17:28:15 +00:00
|
|
|
|
|
|
|
Cycles perform_bus_operation(CPU::MOS6502Esque::BusOperation operation, uint32_t address, uint8_t *value) {
|
|
|
|
// Record the basics of the operation.
|
|
|
|
auto &cycle = cycles.emplace_back();
|
|
|
|
cycle.address = address;
|
|
|
|
cycle.operation = operation;
|
2022-06-19 02:00:50 +00:00
|
|
|
cycle.extended_bus = processor.get_extended_bus_output();
|
2022-06-18 17:28:15 +00:00
|
|
|
|
|
|
|
// Perform the operation, and fill in the cycle's value.
|
|
|
|
using BusOperation = CPU::MOS6502Esque::BusOperation;
|
|
|
|
auto ram_value = ram.find(address);
|
|
|
|
switch(operation) {
|
|
|
|
case BusOperation::ReadOpcode:
|
2022-06-18 20:26:40 +00:00
|
|
|
--opcodes_remaining;
|
|
|
|
if(!opcodes_remaining) {
|
|
|
|
cycles.pop_back();
|
|
|
|
throw StopException();
|
|
|
|
}
|
2022-06-18 17:28:15 +00:00
|
|
|
case BusOperation::Read:
|
|
|
|
case BusOperation::ReadProgram:
|
|
|
|
case BusOperation::ReadVector:
|
|
|
|
if(ram_value != ram.end()) {
|
|
|
|
cycle.value = *value = ram_value->second;
|
|
|
|
} else {
|
|
|
|
cycle.value = *value = uint8_t(rand() >> 8);
|
2022-06-22 01:47:18 +00:00
|
|
|
inventions[address] = ram[address] = *cycle.value;
|
2022-06-18 17:28:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BusOperation::Write:
|
|
|
|
cycle.value = ram[address] = *value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't occupy any bonus time.
|
|
|
|
return Cycles(1);
|
|
|
|
}
|
|
|
|
|
2022-06-19 02:00:50 +00:00
|
|
|
void setup(uint8_t opcode) {
|
2022-06-18 20:26:40 +00:00
|
|
|
ram.clear();
|
|
|
|
inventions.clear();
|
|
|
|
cycles.clear();
|
|
|
|
|
|
|
|
using Register = CPU::MOS6502Esque::Register;
|
|
|
|
const uint32_t pc =
|
|
|
|
processor.get_value_of_register(Register::ProgramCounter) |
|
2022-06-18 23:21:56 +00:00
|
|
|
(processor.get_value_of_register(Register::ProgramBank) << 16);
|
2022-06-18 20:26:40 +00:00
|
|
|
inventions[pc] = ram[pc] = opcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int opcodes_remaining = 0;
|
2022-06-18 17:28:15 +00:00
|
|
|
|
|
|
|
struct Cycle {
|
|
|
|
CPU::MOS6502Esque::BusOperation operation;
|
|
|
|
uint32_t address;
|
2022-06-22 01:47:18 +00:00
|
|
|
std::optional<uint8_t> value;
|
2022-06-19 02:00:50 +00:00
|
|
|
int extended_bus;
|
2022-06-18 17:28:15 +00:00
|
|
|
};
|
|
|
|
std::vector<Cycle> cycles;
|
2022-06-19 02:00:50 +00:00
|
|
|
|
|
|
|
CPU::WDC65816::Processor<BusHandler, false> processor;
|
|
|
|
|
|
|
|
BusHandler() : processor(*this) {
|
|
|
|
// Never run the official reset procedure.
|
|
|
|
processor.set_power_on(false);
|
|
|
|
|
|
|
|
}
|
2022-06-18 17:28:15 +00:00
|
|
|
};
|
|
|
|
|
2022-06-19 02:00:50 +00:00
|
|
|
template <typename Processor> void print_registers(FILE *file, const Processor &processor, int pc_offset) {
|
2022-06-19 01:32:50 +00:00
|
|
|
using Register = CPU::MOS6502Esque::Register;
|
2022-06-19 02:00:50 +00:00
|
|
|
fprintf(file, "\"pc\": %d, ", (processor.get_value_of_register(Register::ProgramCounter) + pc_offset) & 65535);
|
|
|
|
fprintf(file, "\"s\": %d, ", processor.get_value_of_register(Register::StackPointer));
|
|
|
|
fprintf(file, "\"p\": %d, ", processor.get_value_of_register(Register::Flags));
|
|
|
|
fprintf(file, "\"a\": %d, ", processor.get_value_of_register(Register::A));
|
|
|
|
fprintf(file, "\"x\": %d, ", processor.get_value_of_register(Register::X));
|
|
|
|
fprintf(file, "\"y\": %d, ", processor.get_value_of_register(Register::Y));
|
|
|
|
fprintf(file, "\"dbr\": %d, ", processor.get_value_of_register(Register::DataBank));
|
|
|
|
fprintf(file, "\"d\": %d, ", processor.get_value_of_register(Register::Direct));
|
|
|
|
fprintf(file, "\"pbr\": %d, ", processor.get_value_of_register(Register::ProgramBank));
|
|
|
|
fprintf(file, "\"e\": %d, ", processor.get_value_of_register(Register::EmulationFlag));
|
2022-06-19 01:32:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 02:00:50 +00:00
|
|
|
void print_ram(FILE *file, const std::unordered_map<uint32_t, uint8_t> &data) {
|
|
|
|
fprintf(file, "\"ram\": [");
|
2022-06-19 01:32:50 +00:00
|
|
|
bool is_first = true;
|
|
|
|
for(const auto &pair: data) {
|
2022-06-19 02:00:50 +00:00
|
|
|
if(!is_first) fprintf(file, ", ");
|
2022-06-19 01:32:50 +00:00
|
|
|
is_first = false;
|
2022-06-19 02:00:50 +00:00
|
|
|
fprintf(file, "[%d, %d]", pair.first, pair.second);
|
2022-06-19 01:32:50 +00:00
|
|
|
}
|
2022-06-19 02:00:50 +00:00
|
|
|
fprintf(file, "]");
|
2022-06-19 01:32:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 17:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - New test generator.
|
|
|
|
|
|
|
|
@interface TestGenerator : NSObject
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation TestGenerator
|
|
|
|
|
|
|
|
- (void)generate {
|
|
|
|
BusHandler handler;
|
2022-06-18 20:26:40 +00:00
|
|
|
|
2022-06-18 23:21:56 +00:00
|
|
|
// Make tests repeatable, at least for any given instance of
|
|
|
|
// the runtime.
|
|
|
|
srand(65816);
|
|
|
|
|
2022-06-19 02:00:50 +00:00
|
|
|
NSString *const tempDir = NSTemporaryDirectory();
|
|
|
|
NSLog(@"Outputting to %@", tempDir);
|
|
|
|
|
2022-06-18 17:28:15 +00:00
|
|
|
for(int operation = 0; operation < 512; operation++) {
|
|
|
|
const bool is_emulated = operation & 256;
|
|
|
|
const uint8_t opcode = operation & 255;
|
|
|
|
|
2022-06-19 02:00:50 +00:00
|
|
|
NSString *const targetName = [NSString stringWithFormat:@"%@%02x.%c.json", tempDir, opcode, is_emulated ? 'e' : 'n'];
|
|
|
|
FILE *const target = fopen(targetName.UTF8String, "wt");
|
|
|
|
|
|
|
|
bool is_first_test = true;
|
|
|
|
fprintf(target, "[");
|
|
|
|
for(int test = 0; test < 10'000; test++) {
|
|
|
|
if(!is_first_test) fprintf(target, ",\n");
|
|
|
|
is_first_test = false;
|
|
|
|
|
2022-06-19 01:32:50 +00:00
|
|
|
// Ensure processor's next action is an opcode fetch.
|
2022-06-19 02:00:50 +00:00
|
|
|
handler.processor.restart_operation_fetch();
|
2022-06-19 01:32:50 +00:00
|
|
|
|
|
|
|
// Randomise most of the processor state...
|
|
|
|
using Register = CPU::MOS6502Esque::Register;
|
2022-06-19 02:00:50 +00:00
|
|
|
handler.processor.set_value_of_register(Register::A, rand() >> 8);
|
|
|
|
handler.processor.set_value_of_register(Register::Flags, rand() >> 8);
|
|
|
|
handler.processor.set_value_of_register(Register::X, rand() >> 8);
|
|
|
|
handler.processor.set_value_of_register(Register::Y, rand() >> 8);
|
|
|
|
handler.processor.set_value_of_register(Register::ProgramCounter, rand() >> 8);
|
|
|
|
handler.processor.set_value_of_register(Register::StackPointer, rand() >> 8);
|
|
|
|
handler.processor.set_value_of_register(Register::DataBank, rand() >> 8);
|
|
|
|
handler.processor.set_value_of_register(Register::ProgramBank, rand() >> 8);
|
|
|
|
handler.processor.set_value_of_register(Register::Direct, rand() >> 8);
|
2022-06-19 01:32:50 +00:00
|
|
|
|
|
|
|
// ... except for emulation mode, which is a given.
|
|
|
|
// And is set last to ensure proper internal state is applied.
|
2022-06-19 02:00:50 +00:00
|
|
|
handler.processor.set_value_of_register(Register::EmulationFlag, is_emulated);
|
2022-06-19 01:32:50 +00:00
|
|
|
|
|
|
|
// Establish the opcode.
|
2022-06-19 02:00:50 +00:00
|
|
|
handler.setup(opcode);
|
2022-06-19 01:32:50 +00:00
|
|
|
|
|
|
|
// Dump initial state.
|
2022-06-19 02:00:50 +00:00
|
|
|
fprintf(target, "{ \"name\": \"%02x %c %d\", ", opcode, is_emulated ? 'e' : 'n', test + 1);
|
|
|
|
fprintf(target, "\"initial\": {");
|
|
|
|
print_registers(target, handler.processor, 0);
|
2022-06-19 01:32:50 +00:00
|
|
|
|
|
|
|
// Run to the second opcode fetch.
|
|
|
|
handler.opcodes_remaining = 2;
|
|
|
|
try {
|
2022-06-19 02:00:50 +00:00
|
|
|
handler.processor.run_for(Cycles(100));
|
2022-06-19 01:32:50 +00:00
|
|
|
} catch (const StopException &) {}
|
|
|
|
|
|
|
|
// Dump all inventions as initial memory state.
|
2022-06-19 02:00:50 +00:00
|
|
|
print_ram(target, handler.inventions);
|
2022-06-19 01:32:50 +00:00
|
|
|
|
|
|
|
// Dump final state.
|
2022-06-19 02:00:50 +00:00
|
|
|
fprintf(target, "}, \"final\": {");
|
|
|
|
print_registers(target, handler.processor, -1);
|
|
|
|
print_ram(target, handler.ram);
|
|
|
|
fprintf(target, "}, ");
|
2022-06-19 01:32:50 +00:00
|
|
|
|
|
|
|
// Append cycles.
|
2022-06-19 02:00:50 +00:00
|
|
|
fprintf(target, "\"cycles\": [");
|
2022-06-19 01:32:50 +00:00
|
|
|
|
2022-06-19 02:00:50 +00:00
|
|
|
bool is_first_cycle = true;
|
2022-06-19 01:32:50 +00:00
|
|
|
for(const auto &cycle: handler.cycles) {
|
2022-06-19 02:00:50 +00:00
|
|
|
if(!is_first_cycle) fprintf(target, ",");
|
|
|
|
is_first_cycle = false;
|
2022-06-19 01:32:50 +00:00
|
|
|
|
|
|
|
bool vda = false;
|
|
|
|
bool vpa = false;
|
|
|
|
bool vpb = false;
|
|
|
|
bool read = false;
|
|
|
|
bool wait = false;
|
|
|
|
using BusOperation = CPU::MOS6502Esque::BusOperation;
|
|
|
|
switch(cycle.operation) {
|
|
|
|
case BusOperation::Read: read = vda = true; break;
|
|
|
|
case BusOperation::ReadOpcode: read = vda = vpa = true; break;
|
|
|
|
case BusOperation::ReadProgram: read = vpa = true; break;
|
|
|
|
case BusOperation::ReadVector: read = vpb = true; break;
|
|
|
|
case BusOperation::InternalOperationRead: read = true; break;
|
|
|
|
|
|
|
|
case BusOperation::Write: vda = true; break;
|
|
|
|
case BusOperation::InternalOperationWrite: break;
|
|
|
|
|
|
|
|
case BusOperation::None:
|
|
|
|
case BusOperation::Ready: wait = true; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
2022-06-18 17:28:15 +00:00
|
|
|
|
2022-06-19 02:00:50 +00:00
|
|
|
using ExtendedBusOutput = CPU::WDC65816::ExtendedBusOutput;
|
|
|
|
const bool emulation = cycle.extended_bus & ExtendedBusOutput::Emulation;
|
|
|
|
const bool memory_size = cycle.extended_bus & ExtendedBusOutput::MemorySize;
|
|
|
|
const bool index_size = cycle.extended_bus & ExtendedBusOutput::IndexSize;
|
|
|
|
const bool memory_lock = cycle.extended_bus & ExtendedBusOutput::MemoryLock;
|
|
|
|
|
2022-06-22 01:47:18 +00:00
|
|
|
fprintf(target, "[%d, ", cycle.address);
|
|
|
|
if(cycle.value) {
|
|
|
|
fprintf(target, "%d, ", *cycle.value);
|
|
|
|
} else {
|
|
|
|
fprintf(target, "null, ");
|
|
|
|
}
|
|
|
|
fprintf(target, "\"%c%c%c%c%c%c%c%c\"]",
|
2022-06-19 01:32:50 +00:00
|
|
|
vda ? 'd' : '-',
|
|
|
|
vpa ? 'p' : '-',
|
|
|
|
vpb ? 'v' : '-',
|
2022-06-19 02:00:50 +00:00
|
|
|
wait ? '-' : (read ? 'r' : 'w'),
|
|
|
|
wait ? '-' : (emulation ? 'e' : '-'),
|
|
|
|
wait ? '-' : (memory_size ? 'm' : '-'),
|
2022-06-19 10:58:23 +00:00
|
|
|
wait ? '-' : (index_size ? 'x' : '-'),
|
2022-06-19 02:00:50 +00:00
|
|
|
wait ? '-' : (memory_lock ? 'l' : '-')
|
|
|
|
);
|
2022-06-19 01:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate object.
|
2022-06-19 02:00:50 +00:00
|
|
|
fprintf(target, "]}");
|
2022-06-19 01:32:50 +00:00
|
|
|
}
|
2022-06-19 02:00:50 +00:00
|
|
|
|
|
|
|
fprintf(target, "]");
|
|
|
|
fclose(target);
|
2022-06-18 20:26:40 +00:00
|
|
|
}
|
2022-06-18 17:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
// MARK: - Existing test evaluator.
|
|
|
|
|
|
|
|
@interface WDC65816ComparativeTests : XCTestCase
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation WDC65816ComparativeTests
|
|
|
|
|
|
|
|
// A generator for tests; not intended to be a permanent fixture.
|
|
|
|
- (void)testGenerate {
|
|
|
|
[[[TestGenerator alloc] init] generate];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|