2023-09-13 19:53:38 +00:00
|
|
|
//
|
|
|
|
// 8088Tests.m
|
|
|
|
// Clock SignalTests
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 13/09/2023.
|
|
|
|
// Copyright © 2023 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <iostream>
|
2023-09-17 20:22:17 +00:00
|
|
|
#include <sstream>
|
2023-09-13 19:53:38 +00:00
|
|
|
#include <fstream>
|
2023-11-02 19:37:59 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2023-09-13 19:53:38 +00:00
|
|
|
|
|
|
|
#include "NSData+dataWithContentsOfGZippedFile.h"
|
|
|
|
|
2023-11-06 02:42:22 +00:00
|
|
|
#include "../../../InstructionSets/x86/AccessType.hpp"
|
2023-09-13 20:00:16 +00:00
|
|
|
#include "../../../InstructionSets/x86/Decoder.hpp"
|
2023-10-05 18:37:58 +00:00
|
|
|
#include "../../../InstructionSets/x86/Perform.hpp"
|
2023-11-02 20:55:38 +00:00
|
|
|
#include "../../../InstructionSets/x86/Flags.hpp"
|
2023-10-06 02:27:52 +00:00
|
|
|
#include "../../../Numeric/RegisterSizes.hpp"
|
2023-09-13 20:00:16 +00:00
|
|
|
|
2023-09-13 19:53:38 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// The tests themselves are not duplicated in this repository;
|
|
|
|
// provide their real path here.
|
2023-11-01 01:55:22 +00:00
|
|
|
constexpr char TestSuiteHome[] = "/Users/tharte/Projects/ProcessorTests/8088/v1";
|
2023-09-13 19:53:38 +00:00
|
|
|
|
2023-11-02 20:55:38 +00:00
|
|
|
using Flags = InstructionSet::x86::Flags;
|
2023-10-06 15:31:45 +00:00
|
|
|
struct Registers {
|
2023-11-14 16:39:44 +00:00
|
|
|
public:
|
|
|
|
static constexpr bool is_32bit = false;
|
|
|
|
|
|
|
|
uint8_t &al() { return ax_.halves.low; }
|
|
|
|
uint8_t &ah() { return ax_.halves.high; }
|
|
|
|
uint16_t &ax() { return ax_.full; }
|
|
|
|
|
|
|
|
CPU::RegisterPair16 &axp() { return ax_; }
|
|
|
|
|
|
|
|
uint8_t &cl() { return cx_.halves.low; }
|
|
|
|
uint8_t &ch() { return cx_.halves.high; }
|
|
|
|
uint16_t &cx() { return cx_.full; }
|
|
|
|
|
|
|
|
uint8_t &dl() { return dx_.halves.low; }
|
|
|
|
uint8_t &dh() { return dx_.halves.high; }
|
|
|
|
uint16_t &dx() { return dx_.full; }
|
|
|
|
|
|
|
|
uint8_t &bl() { return bx_.halves.low; }
|
|
|
|
uint8_t &bh() { return bx_.halves.high; }
|
|
|
|
uint16_t &bx() { return bx_.full; }
|
|
|
|
|
|
|
|
uint16_t &sp() { return sp_; }
|
|
|
|
uint16_t &bp() { return bp_; }
|
|
|
|
uint16_t &si() { return si_; }
|
|
|
|
uint16_t &di() { return di_; }
|
|
|
|
|
|
|
|
uint16_t &ip() { return ip_; }
|
|
|
|
|
|
|
|
uint16_t &es() { return es_; }
|
|
|
|
uint16_t &cs() { return cs_; }
|
|
|
|
uint16_t &ds() { return ds_; }
|
|
|
|
uint16_t &ss() { return ss_; }
|
|
|
|
|
|
|
|
const uint16_t es() const { return es_; }
|
|
|
|
const uint16_t cs() const { return cs_; }
|
|
|
|
const uint16_t ds() const { return ds_; }
|
|
|
|
const uint16_t ss() const { return ss_; }
|
|
|
|
|
|
|
|
bool operator ==(const Registers &rhs) const {
|
|
|
|
return
|
|
|
|
ax_.full == rhs.ax_.full &&
|
|
|
|
cx_.full == rhs.cx_.full &&
|
|
|
|
dx_.full == rhs.dx_.full &&
|
|
|
|
bx_.full == rhs.bx_.full &&
|
|
|
|
sp_ == rhs.sp_ &&
|
|
|
|
bp_ == rhs.bp_ &&
|
|
|
|
si_ == rhs.si_ &&
|
|
|
|
di_ == rhs.di_ &&
|
|
|
|
es_ == rhs.es_ &&
|
|
|
|
cs_ == rhs.cs_ &&
|
|
|
|
ds_ == rhs.ds_ &&
|
|
|
|
si_ == rhs.si_ &&
|
|
|
|
ip_ == rhs.ip_;
|
|
|
|
}
|
|
|
|
|
2023-11-15 20:58:49 +00:00
|
|
|
private:
|
2023-11-14 16:39:44 +00:00
|
|
|
CPU::RegisterPair16 ax_;
|
|
|
|
CPU::RegisterPair16 cx_;
|
|
|
|
CPU::RegisterPair16 dx_;
|
|
|
|
CPU::RegisterPair16 bx_;
|
|
|
|
|
|
|
|
uint16_t sp_;
|
|
|
|
uint16_t bp_;
|
|
|
|
uint16_t si_;
|
|
|
|
uint16_t di_;
|
|
|
|
uint16_t es_, cs_, ds_, ss_;
|
2023-11-15 20:58:49 +00:00
|
|
|
uint16_t ip_;
|
2023-10-06 15:31:45 +00:00
|
|
|
};
|
2023-11-14 15:56:00 +00:00
|
|
|
class Segments {
|
|
|
|
public:
|
|
|
|
Segments(const Registers ®isters) : registers_(registers) {}
|
|
|
|
|
|
|
|
using Source = InstructionSet::x86::Source;
|
|
|
|
|
|
|
|
/// Posted by @c perform after any operation which *might* have affected a segment register.
|
|
|
|
void did_update(Source segment) {
|
|
|
|
switch(segment) {
|
|
|
|
default: break;
|
|
|
|
case Source::ES: es_base_ = registers_.es() << 4; break;
|
|
|
|
case Source::CS: cs_base_ = registers_.cs() << 4; break;
|
|
|
|
case Source::DS: ds_base_ = registers_.ds() << 4; break;
|
|
|
|
case Source::SS: ss_base_ = registers_.ss() << 4; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
did_update(Source::ES);
|
|
|
|
did_update(Source::CS);
|
|
|
|
did_update(Source::DS);
|
|
|
|
did_update(Source::SS);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t es_base_, cs_base_, ds_base_, ss_base_;
|
|
|
|
|
|
|
|
bool operator ==(const Segments &rhs) const {
|
|
|
|
return
|
2023-11-14 16:14:28 +00:00
|
|
|
es_base_ == rhs.es_base_ &&
|
|
|
|
cs_base_ == rhs.cs_base_ &&
|
|
|
|
ds_base_ == rhs.ds_base_ &&
|
|
|
|
ss_base_ == rhs.ss_base_;
|
2023-11-14 15:56:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Registers ®isters_;
|
|
|
|
};
|
2023-10-06 15:31:45 +00:00
|
|
|
struct Memory {
|
2023-11-02 18:25:13 +00:00
|
|
|
public:
|
|
|
|
using AccessType = InstructionSet::x86::AccessType;
|
2023-10-06 20:32:35 +00:00
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
// Constructor.
|
2023-11-14 15:56:00 +00:00
|
|
|
Memory(Registers ®isters, const Segments &segments) : registers_(registers), segments_(segments) {
|
2023-11-02 18:25:13 +00:00
|
|
|
memory.resize(1024*1024);
|
|
|
|
}
|
2023-10-08 19:44:11 +00:00
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
// Initialisation.
|
|
|
|
void clear() {
|
|
|
|
tags.clear();
|
|
|
|
}
|
2023-10-08 19:59:30 +00:00
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
void seed(uint32_t address, uint8_t value) {
|
|
|
|
memory[address] = value;
|
|
|
|
tags[address] = Tag::Seeded;
|
2023-10-06 15:31:45 +00:00
|
|
|
}
|
2023-10-09 02:11:05 +00:00
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
void touch(uint32_t address) {
|
|
|
|
tags[address] = Tag::AccessExpected;
|
|
|
|
}
|
2023-10-06 17:22:35 +00:00
|
|
|
|
2023-11-05 02:22:50 +00:00
|
|
|
//
|
2023-11-02 18:25:13 +00:00
|
|
|
// Preauthorisation call-ins.
|
2023-11-05 02:22:50 +00:00
|
|
|
//
|
2023-11-02 19:37:59 +00:00
|
|
|
void preauthorise_stack_write(uint32_t length) {
|
2023-11-14 16:39:44 +00:00
|
|
|
uint16_t sp = registers_.sp();
|
2023-11-02 19:37:59 +00:00
|
|
|
while(length--) {
|
|
|
|
--sp;
|
|
|
|
preauthorise(InstructionSet::x86::Source::SS, sp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void preauthorise_stack_read(uint32_t length) {
|
2023-11-14 16:39:44 +00:00
|
|
|
uint16_t sp = registers_.sp();
|
2023-11-02 19:37:59 +00:00
|
|
|
while(length--) {
|
|
|
|
preauthorise(InstructionSet::x86::Source::SS, sp);
|
|
|
|
++sp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void preauthorise_read(InstructionSet::x86::Source segment, uint16_t start, uint32_t length) {
|
|
|
|
while(length--) {
|
|
|
|
preauthorise(segment, start);
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void preauthorise_read(uint32_t start, uint32_t length) {
|
|
|
|
while(length--) {
|
|
|
|
preauthorise(start);
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 18:25:13 +00:00
|
|
|
|
2023-11-03 19:36:30 +00:00
|
|
|
//
|
2023-11-02 18:25:13 +00:00
|
|
|
// Access call-ins.
|
2023-11-03 19:36:30 +00:00
|
|
|
//
|
2023-11-02 18:25:13 +00:00
|
|
|
|
|
|
|
// Accesses an address based on segment:offset.
|
|
|
|
template <typename IntT, AccessType type>
|
2023-11-07 19:03:20 +00:00
|
|
|
typename InstructionSet::x86::Accessor<IntT, type>::type access(InstructionSet::x86::Source segment, uint16_t offset) {
|
|
|
|
return access<IntT, type>(segment, offset, Tag::Accessed);
|
2023-10-23 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
// Accesses an address based on physical location.
|
|
|
|
template <typename IntT, AccessType type>
|
2023-11-07 19:03:20 +00:00
|
|
|
typename InstructionSet::x86::Accessor<IntT, type>::type access(uint32_t address) {
|
2023-11-02 18:25:13 +00:00
|
|
|
return access<IntT, type>(address, Tag::Accessed);
|
2023-10-08 19:59:30 +00:00
|
|
|
}
|
2023-10-06 20:32:35 +00:00
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
template <typename IntT>
|
|
|
|
void write_back() {
|
|
|
|
if constexpr (std::is_same_v<IntT, uint16_t>) {
|
|
|
|
if(write_back_address_[0] != NoWriteBack) {
|
|
|
|
memory[write_back_address_[0]] = write_back_value_ & 0xff;
|
|
|
|
memory[write_back_address_[1]] = write_back_value_ >> 8;
|
|
|
|
write_back_address_[0] = 0;
|
|
|
|
}
|
2023-10-09 02:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-31 19:06:19 +00:00
|
|
|
|
2023-11-03 19:36:30 +00:00
|
|
|
//
|
|
|
|
// Direct write.
|
|
|
|
//
|
|
|
|
template <typename IntT>
|
|
|
|
void preauthorised_write(InstructionSet::x86::Source segment, uint16_t offset, IntT value) {
|
|
|
|
if(!test_preauthorisation(address(segment, offset))) {
|
|
|
|
printf("Non-preauthorised access\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes can be written without further ado.
|
|
|
|
if constexpr (std::is_same_v<IntT, uint8_t>) {
|
|
|
|
memory[address(segment, offset) & 0xf'ffff] = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Words that straddle the segment end must be split in two.
|
|
|
|
if(offset == 0xffff) {
|
|
|
|
memory[address(segment, offset) & 0xf'ffff] = value & 0xff;
|
|
|
|
memory[address(segment, 0x0000) & 0xf'ffff] = value >> 8;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t target = address(segment, offset) & 0xf'ffff;
|
|
|
|
|
|
|
|
// Words that straddle the end of physical RAM must also be split in two.
|
|
|
|
if(target == 0xf'ffff) {
|
|
|
|
memory[0xf'ffff] = value & 0xff;
|
|
|
|
memory[0x0'0000] = value >> 8;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's safe just to write then.
|
|
|
|
*reinterpret_cast<uint16_t *>(&memory[target]) = value;
|
|
|
|
}
|
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
private:
|
|
|
|
enum class Tag {
|
|
|
|
Seeded,
|
|
|
|
AccessExpected,
|
|
|
|
Accessed,
|
|
|
|
};
|
|
|
|
|
2023-11-02 19:37:59 +00:00
|
|
|
std::unordered_set<uint32_t> preauthorisations;
|
2023-11-02 18:25:13 +00:00
|
|
|
std::unordered_map<uint32_t, Tag> tags;
|
|
|
|
std::vector<uint8_t> memory;
|
2023-11-14 16:39:44 +00:00
|
|
|
Registers ®isters_;
|
2023-11-14 15:56:00 +00:00
|
|
|
const Segments &segments_;
|
2023-11-02 18:25:13 +00:00
|
|
|
|
2023-11-02 19:37:59 +00:00
|
|
|
void preauthorise(uint32_t address) {
|
|
|
|
preauthorisations.insert(address);
|
|
|
|
}
|
|
|
|
void preauthorise(InstructionSet::x86::Source segment, uint16_t address) {
|
|
|
|
preauthorise((segment_base(segment) + address) & 0xf'ffff);
|
|
|
|
}
|
|
|
|
bool test_preauthorisation(uint32_t address) {
|
|
|
|
auto authorisation = preauthorisations.find(address);
|
|
|
|
if(authorisation == preauthorisations.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
preauthorisations.erase(authorisation);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
uint32_t segment_base(InstructionSet::x86::Source segment) {
|
|
|
|
using Source = InstructionSet::x86::Source;
|
|
|
|
switch(segment) {
|
2023-11-14 15:56:00 +00:00
|
|
|
default: return segments_.ds_base_;
|
|
|
|
case Source::ES: return segments_.es_base_;
|
|
|
|
case Source::CS: return segments_.cs_base_;
|
|
|
|
case Source::SS: return segments_.ss_base_;
|
2023-11-02 18:25:13 +00:00
|
|
|
}
|
2023-11-01 00:28:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-03 19:36:30 +00:00
|
|
|
uint32_t address(InstructionSet::x86::Source segment, uint16_t offset) {
|
|
|
|
return (segment_base(segment) + offset) & 0xf'ffff;
|
|
|
|
}
|
|
|
|
|
2023-10-09 02:11:05 +00:00
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
// Entry point used by the flow controller so that it can mark up locations at which the flags were written,
|
|
|
|
// so that defined-flag-only masks can be applied while verifying RAM contents.
|
|
|
|
template <typename IntT, AccessType type>
|
2023-11-07 19:03:20 +00:00
|
|
|
typename InstructionSet::x86::Accessor<IntT, type>::type access(InstructionSet::x86::Source segment, uint16_t offset, Tag tag) {
|
2023-11-03 19:36:30 +00:00
|
|
|
const uint32_t physical_address = address(segment, offset);
|
2023-11-07 19:03:20 +00:00
|
|
|
|
|
|
|
if constexpr (std::is_same_v<IntT, uint16_t>) {
|
|
|
|
// If this is a 16-bit access that runs past the end of the segment, it'll wrap back
|
|
|
|
// to the start. So the 16-bit value will need to be a local cache.
|
|
|
|
if(offset == 0xffff) {
|
|
|
|
return split_word<type>(physical_address, address(segment, 0), tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
return access<IntT, type>(physical_address, tag);
|
|
|
|
}
|
2023-11-01 21:03:23 +00:00
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
// An additional entry point for the flow controller; on the original 8086 interrupt vectors aren't relative
|
2023-11-11 03:56:00 +00:00
|
|
|
// to a segment, they're just at an absolute location.
|
2023-11-02 18:25:13 +00:00
|
|
|
template <typename IntT, AccessType type>
|
2023-11-07 19:03:20 +00:00
|
|
|
typename InstructionSet::x86::Accessor<IntT, type>::type access(uint32_t address, Tag tag) {
|
2023-11-03 19:36:30 +00:00
|
|
|
if constexpr (type == AccessType::PreauthorisedRead) {
|
2023-11-02 19:37:59 +00:00
|
|
|
if(!test_preauthorisation(address)) {
|
|
|
|
printf("Non preauthorised access\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 19:03:20 +00:00
|
|
|
for(size_t c = 0; c < sizeof(IntT); c++) {
|
|
|
|
tags[(address + c) & 0xf'ffff] = tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispense with the single-byte case trivially.
|
|
|
|
if constexpr (std::is_same_v<IntT, uint8_t>) {
|
|
|
|
return memory[address];
|
|
|
|
} else if(address != 0xf'ffff) {
|
|
|
|
return *reinterpret_cast<IntT *>(&memory[address]);
|
|
|
|
} else {
|
|
|
|
return split_word<type>(address, 0, tag);
|
2023-11-02 18:25:13 +00:00
|
|
|
}
|
2023-11-07 19:03:20 +00:00
|
|
|
}
|
2023-11-02 18:25:13 +00:00
|
|
|
|
2023-11-07 19:03:20 +00:00
|
|
|
template <AccessType type>
|
|
|
|
typename InstructionSet::x86::Accessor<uint16_t, type>::type
|
|
|
|
split_word(uint32_t low_address, uint32_t high_address, Tag tag) {
|
|
|
|
if constexpr (is_writeable(type)) {
|
|
|
|
write_back_address_[0] = low_address;
|
|
|
|
write_back_address_[1] = high_address;
|
|
|
|
tags[low_address] = tag;
|
|
|
|
tags[high_address] = tag;
|
2023-11-07 19:38:23 +00:00
|
|
|
|
|
|
|
// Prepopulate only if this is a modify.
|
|
|
|
if constexpr (type == AccessType::ReadModifyWrite) {
|
|
|
|
write_back_value_ = memory[write_back_address_[0]] | (memory[write_back_address_[1]] << 8);
|
|
|
|
}
|
|
|
|
|
2023-11-07 19:03:20 +00:00
|
|
|
return write_back_value_;
|
|
|
|
} else {
|
|
|
|
return memory[low_address] | (memory[high_address] << 8);
|
2023-10-09 02:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
static constexpr uint32_t NoWriteBack = 0; // A low byte address of 0 can't require write-back.
|
|
|
|
uint32_t write_back_address_[2] = {NoWriteBack, NoWriteBack};
|
|
|
|
uint16_t write_back_value_;
|
2023-10-06 15:31:45 +00:00
|
|
|
};
|
|
|
|
struct IO {
|
2023-10-22 02:37:25 +00:00
|
|
|
template <typename IntT> void out([[maybe_unused]] uint16_t port, [[maybe_unused]] IntT value) {}
|
|
|
|
template <typename IntT> IntT in([[maybe_unused]] uint16_t port) { return IntT(~0); }
|
2023-10-06 15:31:45 +00:00
|
|
|
};
|
2023-10-06 17:22:35 +00:00
|
|
|
class FlowController {
|
|
|
|
public:
|
2023-11-15 20:58:49 +00:00
|
|
|
FlowController(Registers ®isters, Segments &segments) :
|
|
|
|
registers_(registers), segments_(segments) {}
|
2023-10-06 17:22:35 +00:00
|
|
|
|
2023-11-02 03:39:52 +00:00
|
|
|
// Requirements for perform.
|
2023-12-24 19:11:41 +00:00
|
|
|
template <typename AddressT>
|
|
|
|
void jump(AddressT address) {
|
|
|
|
static_assert(std::is_same_v<AddressT, uint16_t>);
|
2023-11-15 20:58:49 +00:00
|
|
|
registers_.ip() = address;
|
2023-10-10 20:27:06 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 19:11:41 +00:00
|
|
|
template <typename AddressT>
|
|
|
|
void jump(uint16_t segment, AddressT address) {
|
|
|
|
static_assert(std::is_same_v<AddressT, uint16_t>);
|
2023-11-15 20:58:49 +00:00
|
|
|
registers_.cs() = segment;
|
2023-11-14 15:56:00 +00:00
|
|
|
segments_.did_update(Segments::Source::CS);
|
2023-11-15 20:58:49 +00:00
|
|
|
registers_.ip() = address;
|
2023-10-18 17:15:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 20:21:04 +00:00
|
|
|
void halt() {}
|
|
|
|
void wait() {}
|
|
|
|
|
2023-10-19 18:07:59 +00:00
|
|
|
void repeat_last() {
|
2023-10-19 18:40:03 +00:00
|
|
|
should_repeat_ = true;
|
|
|
|
}
|
2023-11-02 03:39:52 +00:00
|
|
|
|
|
|
|
// Other actions.
|
|
|
|
void begin_instruction() {
|
|
|
|
should_repeat_ = false;
|
|
|
|
}
|
2023-10-19 18:40:03 +00:00
|
|
|
bool should_repeat() const {
|
|
|
|
return should_repeat_;
|
2023-10-19 18:07:59 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 17:22:35 +00:00
|
|
|
private:
|
|
|
|
Registers ®isters_;
|
2023-11-14 15:56:00 +00:00
|
|
|
Segments &segments_;
|
2023-10-19 18:40:03 +00:00
|
|
|
bool should_repeat_ = false;
|
2023-10-06 15:31:45 +00:00
|
|
|
};
|
|
|
|
|
2023-10-07 18:23:47 +00:00
|
|
|
struct ExecutionSupport {
|
2023-11-02 20:55:38 +00:00
|
|
|
Flags flags;
|
2023-10-07 18:23:47 +00:00
|
|
|
Registers registers;
|
2023-11-14 15:56:00 +00:00
|
|
|
Segments segments;
|
2023-10-07 18:23:47 +00:00
|
|
|
Memory memory;
|
|
|
|
FlowController flow_controller;
|
|
|
|
IO io;
|
2023-11-01 21:03:23 +00:00
|
|
|
static constexpr auto model = InstructionSet::x86::Model::i8086;
|
2023-10-07 18:23:47 +00:00
|
|
|
|
2023-11-14 15:56:00 +00:00
|
|
|
ExecutionSupport():
|
|
|
|
memory(registers, segments),
|
|
|
|
segments(registers),
|
2023-11-15 20:58:49 +00:00
|
|
|
flow_controller(registers, segments) {}
|
2023-10-07 18:23:47 +00:00
|
|
|
|
|
|
|
void clear() {
|
|
|
|
memory.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-07 18:28:44 +00:00
|
|
|
struct FailedExecution {
|
2023-10-16 16:34:11 +00:00
|
|
|
std::string test_name;
|
|
|
|
std::string reason;
|
2023-10-07 18:28:44 +00:00
|
|
|
InstructionSet::x86::Instruction<false> instruction;
|
|
|
|
};
|
2023-10-07 18:23:47 +00:00
|
|
|
|
2023-09-13 19:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@interface i8088Tests : XCTestCase
|
|
|
|
@end
|
|
|
|
|
2023-10-07 18:23:47 +00:00
|
|
|
@implementation i8088Tests {
|
2023-10-07 18:28:44 +00:00
|
|
|
std::vector<FailedExecution> execution_failures;
|
2023-11-09 16:55:36 +00:00
|
|
|
std::vector<FailedExecution> permitted_failures;
|
2023-11-01 21:03:23 +00:00
|
|
|
ExecutionSupport execution_support;
|
2023-10-07 18:23:47 +00:00
|
|
|
}
|
2023-09-13 19:53:38 +00:00
|
|
|
|
|
|
|
- (NSArray<NSString *> *)testFiles {
|
|
|
|
NSString *path = [NSString stringWithUTF8String:TestSuiteHome];
|
2023-09-28 13:43:26 +00:00
|
|
|
NSSet *allowList = [NSSet setWithArray:@[
|
2023-11-09 16:55:36 +00:00
|
|
|
// Current execution failures, albeit all permitted:
|
2023-11-11 03:11:52 +00:00
|
|
|
// @"D4.json.gz", // AAM
|
|
|
|
// @"F6.7.json.gz", // IDIV byte
|
|
|
|
// @"F7.7.json.gz", // IDIV word
|
2023-09-28 13:43:26 +00:00
|
|
|
]];
|
2023-09-23 02:56:33 +00:00
|
|
|
|
2023-09-30 02:28:23 +00:00
|
|
|
NSSet *ignoreList = nil;
|
2023-09-13 19:53:38 +00:00
|
|
|
|
|
|
|
NSArray<NSString *> *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
|
|
|
|
files = [files filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString* evaluatedObject, NSDictionary<NSString *,id> *) {
|
2023-09-28 13:43:26 +00:00
|
|
|
if(allowList.count && ![allowList containsObject:[evaluatedObject lastPathComponent]]) {
|
2023-09-15 19:50:59 +00:00
|
|
|
return NO;
|
2023-09-23 02:56:33 +00:00
|
|
|
}
|
|
|
|
if([ignoreList containsObject:[evaluatedObject lastPathComponent]]) {
|
|
|
|
return NO;
|
2023-09-15 19:50:59 +00:00
|
|
|
}
|
2023-09-13 19:53:38 +00:00
|
|
|
return [evaluatedObject hasSuffix:@"json.gz"];
|
|
|
|
}]];
|
|
|
|
|
|
|
|
NSMutableArray<NSString *> *fullPaths = [[NSMutableArray alloc] init];
|
|
|
|
for(NSString *file in files) {
|
|
|
|
[fullPaths addObject:[path stringByAppendingPathComponent:file]];
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:28:34 +00:00
|
|
|
return [fullPaths sortedArrayUsingSelector:@selector(compare:)];
|
2023-09-13 19:53:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 02:27:52 +00:00
|
|
|
- (NSArray<NSDictionary *> *)testsInFile:(NSString *)file {
|
|
|
|
NSData *data = [NSData dataWithContentsOfGZippedFile:file];
|
|
|
|
return [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
|
|
|
|
}
|
|
|
|
|
2023-10-06 20:32:35 +00:00
|
|
|
- (NSDictionary *)metadata {
|
|
|
|
NSString *path = [[NSString stringWithUTF8String:TestSuiteHome] stringByAppendingPathComponent:@"8088.json"];
|
|
|
|
return [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfGZippedFile:path] options:0 error:nil];
|
|
|
|
}
|
|
|
|
|
2023-10-25 15:21:11 +00:00
|
|
|
- (NSString *)toString:(const std::pair<int, InstructionSet::x86::Instruction<false>> &)instruction offsetLength:(int)offsetLength immediateLength:(int)immediateLength {
|
2023-10-05 02:35:52 +00:00
|
|
|
const auto operation = to_string(instruction, InstructionSet::x86::Model::i8086, offsetLength, immediateLength);
|
2023-09-28 19:41:57 +00:00
|
|
|
return [[NSString stringWithUTF8String:operation.c_str()] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
2023-09-24 19:26:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 02:27:52 +00:00
|
|
|
- (std::vector<uint8_t>)bytes:(NSArray<NSNumber *> *)encoding {
|
2023-09-24 19:26:58 +00:00
|
|
|
std::vector<uint8_t> data;
|
|
|
|
data.reserve(encoding.count);
|
|
|
|
for(NSNumber *number in encoding) {
|
|
|
|
data.push_back([number intValue]);
|
2023-09-17 20:29:04 +00:00
|
|
|
}
|
2023-10-06 02:27:52 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (bool)applyDecodingTest:(NSDictionary *)test file:(NSString *)file assert:(BOOL)assert {
|
|
|
|
InstructionSet::x86::Decoder<InstructionSet::x86::Model::i8086> decoder;
|
|
|
|
|
|
|
|
// Build a vector of the instruction bytes; this makes manual step debugging easier.
|
|
|
|
const auto data = [self bytes:test[@"bytes"]];
|
2023-09-25 15:37:46 +00:00
|
|
|
auto hex_instruction = [&]() -> NSString * {
|
2023-09-24 19:26:58 +00:00
|
|
|
NSMutableString *hexInstruction = [[NSMutableString alloc] init];
|
|
|
|
for(uint8_t byte: data) {
|
|
|
|
[hexInstruction appendFormat:@"%02x ", byte];
|
|
|
|
}
|
2023-09-25 15:37:46 +00:00
|
|
|
return hexInstruction;
|
2023-09-24 19:26:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto decoded = decoder.decode(data.data(), data.size());
|
2023-10-06 02:27:52 +00:00
|
|
|
const bool sizeMatched = decoded.first == data.size();
|
2023-09-29 02:17:14 +00:00
|
|
|
if(assert) {
|
|
|
|
XCTAssert(
|
2023-10-06 02:27:52 +00:00
|
|
|
sizeMatched,
|
2023-09-29 02:17:14 +00:00
|
|
|
"Wrong length of instruction decoded for %@ — decoded %d rather than %lu from %@; file %@",
|
|
|
|
test[@"name"],
|
|
|
|
decoded.first,
|
2023-10-06 02:27:52 +00:00
|
|
|
(unsigned long)data.size(),
|
2023-09-29 02:17:14 +00:00
|
|
|
hex_instruction(),
|
|
|
|
file
|
|
|
|
);
|
|
|
|
}
|
2023-10-06 02:27:52 +00:00
|
|
|
if(!sizeMatched) {
|
2023-09-29 02:04:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-09-17 20:29:04 +00:00
|
|
|
|
2023-09-24 19:26:58 +00:00
|
|
|
// The decoder doesn't preserve the original offset length, which makes no functional difference but
|
|
|
|
// does affect the way that offsets are printed in the test set.
|
2023-09-28 02:18:05 +00:00
|
|
|
NSSet<NSString *> *decodings = [NSSet setWithObjects:
|
2023-10-25 15:21:11 +00:00
|
|
|
[self toString:decoded offsetLength:4 immediateLength:4],
|
|
|
|
[self toString:decoded offsetLength:2 immediateLength:4],
|
|
|
|
[self toString:decoded offsetLength:0 immediateLength:4],
|
|
|
|
[self toString:decoded offsetLength:4 immediateLength:2],
|
|
|
|
[self toString:decoded offsetLength:2 immediateLength:2],
|
|
|
|
[self toString:decoded offsetLength:0 immediateLength:2],
|
2023-09-28 02:18:05 +00:00
|
|
|
nil];
|
2023-09-26 17:21:24 +00:00
|
|
|
|
|
|
|
auto compare_decoding = [&](NSString *name) -> bool {
|
2023-09-28 02:18:05 +00:00
|
|
|
return [decodings containsObject:name];
|
2023-09-26 17:21:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool isEqual = compare_decoding(test[@"name"]);
|
|
|
|
|
2023-09-28 13:23:40 +00:00
|
|
|
// Attempt clerical reconciliation:
|
|
|
|
//
|
2023-10-27 16:54:42 +00:00
|
|
|
// * the test suite retains a distinction between SHL and SAL, which the decoder doesn't;
|
|
|
|
// * the decoder treats INT3 and INT 3 as the same thing; and
|
|
|
|
// * the decoder doesn't record whether a segment override was present, just the final segment.
|
2023-09-28 19:34:33 +00:00
|
|
|
int adjustment = 7;
|
|
|
|
while(!isEqual && adjustment) {
|
2023-09-28 19:41:57 +00:00
|
|
|
NSString *alteredName = [test[@"name"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
2023-09-28 13:23:40 +00:00
|
|
|
|
2023-09-28 19:34:33 +00:00
|
|
|
if(adjustment & 2) {
|
2023-09-28 13:23:40 +00:00
|
|
|
alteredName = [alteredName stringByReplacingOccurrencesOfString:@"shl" withString:@"sal"];
|
|
|
|
}
|
2023-09-28 19:34:33 +00:00
|
|
|
if(adjustment & 1) {
|
2023-10-24 19:09:25 +00:00
|
|
|
alteredName = [alteredName stringByReplacingOccurrencesOfString:@"int3" withString:@"int 3h"];
|
2023-09-28 19:34:33 +00:00
|
|
|
}
|
2023-10-27 16:54:42 +00:00
|
|
|
if(adjustment & 4) {
|
|
|
|
alteredName = [@"ds " stringByAppendingString:alteredName];
|
|
|
|
}
|
2023-09-28 13:23:40 +00:00
|
|
|
|
2023-09-26 17:21:24 +00:00
|
|
|
isEqual = compare_decoding(alteredName);
|
2023-09-28 19:34:33 +00:00
|
|
|
--adjustment;
|
2023-09-26 17:21:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 02:17:14 +00:00
|
|
|
if(assert) {
|
|
|
|
XCTAssert(
|
|
|
|
isEqual,
|
2023-10-24 15:01:38 +00:00
|
|
|
"%@ doesn't match %@ or similar, was %@",
|
2023-09-29 02:17:14 +00:00
|
|
|
test[@"name"],
|
|
|
|
[decodings anyObject],
|
2023-10-24 15:01:38 +00:00
|
|
|
hex_instruction()
|
2023-09-29 02:17:14 +00:00
|
|
|
);
|
|
|
|
}
|
2023-09-16 02:28:30 +00:00
|
|
|
|
2023-09-29 02:04:59 +00:00
|
|
|
return isEqual;
|
2023-09-13 20:00:16 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 20:55:38 +00:00
|
|
|
- (void)populate:(Registers &)registers flags:(Flags &)flags value:(NSDictionary *)value {
|
2023-11-15 20:58:49 +00:00
|
|
|
registers.ax() = [value[@"ax"] intValue];
|
|
|
|
registers.bx() = [value[@"bx"] intValue];
|
|
|
|
registers.cx() = [value[@"cx"] intValue];
|
|
|
|
registers.dx() = [value[@"dx"] intValue];
|
|
|
|
|
|
|
|
registers.bp() = [value[@"bp"] intValue];
|
|
|
|
registers.cs() = [value[@"cs"] intValue];
|
|
|
|
registers.di() = [value[@"di"] intValue];
|
|
|
|
registers.ds() = [value[@"ds"] intValue];
|
|
|
|
registers.es() = [value[@"es"] intValue];
|
|
|
|
registers.si() = [value[@"si"] intValue];
|
|
|
|
registers.sp() = [value[@"sp"] intValue];
|
|
|
|
registers.ss() = [value[@"ss"] intValue];
|
|
|
|
registers.ip() = [value[@"ip"] intValue];
|
2023-10-06 15:31:45 +00:00
|
|
|
|
2023-11-02 20:55:38 +00:00
|
|
|
const uint16_t flags_value = [value[@"flags"] intValue];
|
|
|
|
flags.set(flags_value);
|
2023-10-19 18:07:59 +00:00
|
|
|
|
|
|
|
// Apply a quick test of flag packing/unpacking.
|
|
|
|
constexpr auto defined_flags = static_cast<uint16_t>(
|
2023-11-02 20:55:38 +00:00
|
|
|
InstructionSet::x86::FlagValue::Carry |
|
|
|
|
InstructionSet::x86::FlagValue::Parity |
|
|
|
|
InstructionSet::x86::FlagValue::AuxiliaryCarry |
|
|
|
|
InstructionSet::x86::FlagValue::Zero |
|
|
|
|
InstructionSet::x86::FlagValue::Sign |
|
|
|
|
InstructionSet::x86::FlagValue::Trap |
|
|
|
|
InstructionSet::x86::FlagValue::Interrupt |
|
|
|
|
InstructionSet::x86::FlagValue::Direction |
|
|
|
|
InstructionSet::x86::FlagValue::Overflow
|
2023-10-19 18:07:59 +00:00
|
|
|
);
|
2023-11-02 20:55:38 +00:00
|
|
|
XCTAssert((flags.get() & defined_flags) == (flags_value & defined_flags),
|
|
|
|
"Set flags of %04x was returned as %04x",
|
|
|
|
flags_value & defined_flags,
|
|
|
|
(flags.get() & defined_flags)
|
2023-10-19 18:07:59 +00:00
|
|
|
);
|
2023-10-06 15:31:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 19:40:24 +00:00
|
|
|
- (void)applyExecutionTest:(NSDictionary *)test metadata:(NSDictionary *)metadata {
|
2023-10-06 02:27:52 +00:00
|
|
|
InstructionSet::x86::Decoder<InstructionSet::x86::Model::i8086> decoder;
|
|
|
|
const auto data = [self bytes:test[@"bytes"]];
|
|
|
|
const auto decoded = decoder.decode(data.data(), data.size());
|
|
|
|
|
2023-10-07 18:23:47 +00:00
|
|
|
execution_support.clear();
|
2023-10-06 02:27:52 +00:00
|
|
|
|
2023-10-06 20:32:35 +00:00
|
|
|
const uint16_t flags_mask = metadata[@"flags-mask"] ? [metadata[@"flags-mask"] intValue] : 0xffff;
|
2023-10-08 19:59:30 +00:00
|
|
|
NSDictionary *const initial_state = test[@"initial"];
|
|
|
|
NSDictionary *const final_state = test[@"final"];
|
2023-10-06 20:32:35 +00:00
|
|
|
|
2023-10-06 15:07:33 +00:00
|
|
|
// Apply initial state.
|
2023-11-02 20:55:38 +00:00
|
|
|
Flags initial_flags;
|
2023-10-06 15:31:45 +00:00
|
|
|
for(NSArray<NSNumber *> *ram in initial_state[@"ram"]) {
|
2023-10-08 19:44:11 +00:00
|
|
|
execution_support.memory.seed([ram[0] intValue], [ram[1] intValue]);
|
2023-10-06 15:07:33 +00:00
|
|
|
}
|
2023-10-08 19:59:30 +00:00
|
|
|
for(NSArray<NSNumber *> *ram in final_state[@"ram"]) {
|
|
|
|
execution_support.memory.touch([ram[0] intValue]);
|
|
|
|
}
|
2023-10-10 14:34:18 +00:00
|
|
|
Registers initial_registers;
|
2023-11-02 20:55:38 +00:00
|
|
|
[self populate:initial_registers flags:initial_flags value:initial_state[@"regs"]];
|
|
|
|
execution_support.flags = initial_flags;
|
2023-10-10 14:34:18 +00:00
|
|
|
execution_support.registers = initial_registers;
|
2023-11-14 15:56:00 +00:00
|
|
|
execution_support.segments.reset();
|
2023-10-06 20:32:35 +00:00
|
|
|
|
2023-10-06 15:31:45 +00:00
|
|
|
// Execute instruction.
|
2023-10-20 20:52:47 +00:00
|
|
|
//
|
|
|
|
// TODO: enquire of the actual mechanism of repetition; if it were stateful as below then
|
|
|
|
// would it survive interrupts? So is it just IP adjustment?
|
2023-11-15 20:58:49 +00:00
|
|
|
execution_support.registers.ip() += decoded.first;
|
2023-10-19 18:40:03 +00:00
|
|
|
do {
|
|
|
|
execution_support.flow_controller.begin_instruction();
|
2023-11-01 21:03:23 +00:00
|
|
|
InstructionSet::x86::perform(
|
2023-10-19 18:40:03 +00:00
|
|
|
decoded.second,
|
2023-11-01 21:03:23 +00:00
|
|
|
execution_support
|
2023-10-19 18:40:03 +00:00
|
|
|
);
|
|
|
|
} while (execution_support.flow_controller.should_repeat());
|
2023-10-06 02:27:52 +00:00
|
|
|
|
2023-10-06 15:31:45 +00:00
|
|
|
// Compare final state.
|
|
|
|
Registers intended_registers;
|
2023-11-02 20:55:38 +00:00
|
|
|
InstructionSet::x86::Flags intended_flags;
|
2023-10-06 15:31:45 +00:00
|
|
|
|
|
|
|
bool ramEqual = true;
|
2023-11-02 03:39:52 +00:00
|
|
|
int mask_position = 0;
|
2023-10-06 15:31:45 +00:00
|
|
|
for(NSArray<NSNumber *> *ram in final_state[@"ram"]) {
|
2023-10-06 20:32:35 +00:00
|
|
|
const uint32_t address = [ram[0] intValue];
|
2023-11-02 18:25:13 +00:00
|
|
|
const auto value = execution_support.memory.access<uint8_t, Memory::AccessType::Read>(address);
|
2023-10-06 20:32:35 +00:00
|
|
|
|
2023-11-02 18:25:13 +00:00
|
|
|
if((mask_position != 1) && value == [ram[1] intValue]) {
|
2023-11-02 03:39:52 +00:00
|
|
|
continue;
|
2023-10-06 20:32:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 03:39:52 +00:00
|
|
|
// Consider whether this apparent mismatch might be because flags have been written to memory;
|
|
|
|
// allow only one use of the [16-bit] mask per test.
|
|
|
|
bool matched_with_mask = false;
|
|
|
|
while(mask_position < 2) {
|
|
|
|
const uint8_t mask = mask_position ? (flags_mask >> 8) : (flags_mask & 0xff);
|
|
|
|
++mask_position;
|
2023-11-02 18:25:13 +00:00
|
|
|
if((value & mask) == ([ram[1] intValue] & mask)) {
|
2023-11-02 03:39:52 +00:00
|
|
|
matched_with_mask = true;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-23 14:41:58 +00:00
|
|
|
}
|
2023-11-02 03:39:52 +00:00
|
|
|
if(matched_with_mask) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ramEqual = false;
|
|
|
|
break;
|
2023-10-06 15:31:45 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 15:56:00 +00:00
|
|
|
Segments intended_segments(intended_registers);
|
2023-11-02 20:55:38 +00:00
|
|
|
[self populate:intended_registers flags:intended_flags value:final_state[@"regs"]];
|
2023-11-14 15:56:00 +00:00
|
|
|
intended_segments.reset();
|
|
|
|
|
|
|
|
const bool registersEqual = intended_registers == execution_support.registers && intended_segments == execution_support.segments;
|
2023-11-02 20:55:38 +00:00
|
|
|
const bool flagsEqual = (intended_flags.get() & flags_mask) == (execution_support.flags.get() & flags_mask);
|
2023-10-06 15:31:45 +00:00
|
|
|
|
2023-11-09 16:55:36 +00:00
|
|
|
// Exit if no issues were found.
|
|
|
|
if(flagsEqual && registersEqual && ramEqual) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Presume this is a genuine failure.
|
|
|
|
std::vector<FailedExecution> *failure_list = &execution_failures;
|
|
|
|
|
|
|
|
// Redirect it if it's an acceptable failure.
|
|
|
|
using Operation = InstructionSet::x86::Operation;
|
|
|
|
|
|
|
|
// AAM 00h throws its exception only after modifying flags in an undocumented manner;
|
|
|
|
// I'm not too concerned about this because AAM 00h is an undocumented usage of 00h,
|
|
|
|
// not even supported by NEC amongst others, and the proper exception is being thrown.
|
|
|
|
if(decoded.second.operation() == Operation::AAM && !decoded.second.operand()) {
|
|
|
|
failure_list = &permitted_failures;
|
|
|
|
}
|
|
|
|
|
2023-11-09 17:26:40 +00:00
|
|
|
// IDIV_REP: for reasons I don't understand, sometimes the test set doesn't increment
|
|
|
|
// the IP across a REP_IDIV. I don't think (?) this correlates to real 8086 behaviour.
|
|
|
|
// More research required, but for now I'm not treating this as a roadblock.
|
2023-11-09 16:55:36 +00:00
|
|
|
if(decoded.second.operation() == Operation::IDIV_REP) {
|
|
|
|
Registers advanced_registers = intended_registers;
|
2023-11-15 20:58:49 +00:00
|
|
|
advanced_registers.ip() += decoded.first;
|
2023-11-09 16:55:36 +00:00
|
|
|
if(advanced_registers == execution_support.registers && ramEqual && flagsEqual) {
|
|
|
|
failure_list = &permitted_failures;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 17:26:40 +00:00
|
|
|
// IDIV[_REP] byte: the test cases sometimes throw even when I can't see why they should,
|
|
|
|
// and other x86 emulations also don't throw. I guess — guess! — an 8086-specific oddity
|
|
|
|
// deviates from the x86 average here. So I'm also permitting these for now.
|
|
|
|
if(
|
|
|
|
decoded.second.operation_size() == InstructionSet::x86::DataSize::Byte &&
|
|
|
|
(decoded.second.operation() == Operation::IDIV_REP || decoded.second.operation() == Operation::IDIV)
|
|
|
|
) {
|
|
|
|
if(intended_registers.sp() == execution_support.registers.sp() - 6) {
|
|
|
|
Registers non_exception_registers = intended_registers;
|
|
|
|
non_exception_registers.ip() = execution_support.registers.ip();
|
|
|
|
non_exception_registers.sp() = execution_support.registers.sp();
|
|
|
|
non_exception_registers.ax() = execution_support.registers.ax();
|
|
|
|
non_exception_registers.cs() = execution_support.registers.cs();
|
|
|
|
|
|
|
|
if(non_exception_registers == execution_support.registers) {
|
|
|
|
failure_list = &permitted_failures;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LEA from a register is undefined behaviour and throws on processors beyond the 8086.
|
|
|
|
if(decoded.second.operation() == Operation::LEA && InstructionSet::x86::is_register(decoded.second.source().source())) {
|
|
|
|
failure_list = &permitted_failures;
|
|
|
|
}
|
|
|
|
|
2023-11-09 16:55:36 +00:00
|
|
|
// Record a failure.
|
|
|
|
FailedExecution failure;
|
|
|
|
failure.instruction = decoded.second;
|
|
|
|
failure.test_name = std::string([test[@"name"] UTF8String]);
|
|
|
|
|
|
|
|
NSMutableArray<NSString *> *reasons = [[NSMutableArray alloc] init];
|
|
|
|
if(!flagsEqual) {
|
|
|
|
Flags difference;
|
|
|
|
difference.set((intended_flags.get() ^ execution_support.flags.get()) & flags_mask);
|
|
|
|
[reasons addObject:
|
|
|
|
[NSString stringWithFormat:@"flags differs; errors in %s",
|
|
|
|
difference.to_string().c_str()]];
|
|
|
|
}
|
|
|
|
if(!registersEqual) {
|
|
|
|
NSMutableArray<NSString *> *registers = [[NSMutableArray alloc] init];
|
2023-10-20 21:00:32 +00:00
|
|
|
#define Reg(x) \
|
|
|
|
if(intended_registers.x() != execution_support.registers.x()) \
|
|
|
|
[registers addObject: \
|
|
|
|
[NSString stringWithFormat: \
|
|
|
|
@#x" is %04x rather than %04x", execution_support.registers.x(), intended_registers.x()]];
|
|
|
|
|
2023-11-09 16:55:36 +00:00
|
|
|
Reg(ax);
|
|
|
|
Reg(cx);
|
|
|
|
Reg(dx);
|
|
|
|
Reg(bx);
|
|
|
|
Reg(sp);
|
|
|
|
Reg(bp);
|
|
|
|
Reg(si);
|
|
|
|
Reg(di);
|
|
|
|
Reg(ip);
|
|
|
|
Reg(es);
|
|
|
|
Reg(cs);
|
|
|
|
Reg(ds);
|
|
|
|
Reg(ss);
|
2023-10-20 21:00:32 +00:00
|
|
|
|
|
|
|
#undef Reg
|
2023-11-09 16:55:36 +00:00
|
|
|
[reasons addObject:[NSString stringWithFormat:
|
|
|
|
@"registers don't match: %@", [registers componentsJoinedByString:@", "]
|
|
|
|
]];
|
2023-10-16 16:34:11 +00:00
|
|
|
}
|
2023-11-09 16:55:36 +00:00
|
|
|
if(!ramEqual) {
|
|
|
|
[reasons addObject:@"RAM contents don't match"];
|
|
|
|
}
|
|
|
|
|
|
|
|
failure.reason = std::string([reasons componentsJoinedByString:@"; "].UTF8String);
|
|
|
|
failure_list->push_back(std::move(failure));
|
2023-10-06 02:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)printFailures:(NSArray<NSString *> *)failures {
|
|
|
|
NSLog(
|
|
|
|
@"%ld failures out of %ld tests: %@",
|
|
|
|
failures.count,
|
|
|
|
[self testFiles].count,
|
|
|
|
[failures sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]);
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:53:38 +00:00
|
|
|
- (void)testDecoding {
|
2023-10-06 02:27:52 +00:00
|
|
|
NSMutableArray<NSString *> *failures = [[NSMutableArray alloc] init];
|
2023-10-10 02:12:15 +00:00
|
|
|
for(NSString *file in [self testFiles]) @autoreleasepool {
|
2023-10-06 02:27:52 +00:00
|
|
|
for(NSDictionary *test in [self testsInFile:file]) {
|
2023-09-13 20:08:12 +00:00
|
|
|
// A single failure per instruction is fine.
|
2023-09-29 02:17:14 +00:00
|
|
|
if(![self applyDecodingTest:test file:file assert:YES]) {
|
2023-09-14 13:33:45 +00:00
|
|
|
[failures addObject:file];
|
2023-09-29 02:04:59 +00:00
|
|
|
|
|
|
|
// Attempt a second decoding, to provide a debugger hook.
|
2023-09-29 02:17:14 +00:00
|
|
|
[self applyDecodingTest:test file:file assert:NO];
|
2023-09-13 20:08:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-09-13 20:00:16 +00:00
|
|
|
}
|
2023-09-13 19:53:38 +00:00
|
|
|
}
|
2023-09-14 13:33:45 +00:00
|
|
|
|
2023-10-06 02:27:52 +00:00
|
|
|
[self printFailures:failures];
|
2023-09-13 19:53:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 18:37:58 +00:00
|
|
|
- (void)testExecution {
|
2023-10-06 20:32:35 +00:00
|
|
|
NSDictionary *metadata = [self metadata];
|
2023-10-23 14:02:13 +00:00
|
|
|
NSMutableArray<NSString *> *failures = [[NSMutableArray alloc] init];
|
2023-10-06 20:32:35 +00:00
|
|
|
|
2023-10-10 02:12:15 +00:00
|
|
|
for(NSString *file in [self testFiles]) @autoreleasepool {
|
2023-10-23 14:02:13 +00:00
|
|
|
const auto failures_before = execution_failures.size();
|
|
|
|
|
2023-10-09 02:11:05 +00:00
|
|
|
// Determine the metadata key.
|
2023-10-06 20:32:35 +00:00
|
|
|
NSString *const name = [file lastPathComponent];
|
|
|
|
NSRange first_dot = [name rangeOfString:@"."];
|
|
|
|
NSString *metadata_key = [name substringToIndex:first_dot.location];
|
|
|
|
|
2023-10-09 02:11:05 +00:00
|
|
|
// Grab the metadata. If it wants a reg field, inspect a little further.
|
2023-10-06 20:32:35 +00:00
|
|
|
NSDictionary *test_metadata = metadata[metadata_key];
|
2023-10-09 02:11:05 +00:00
|
|
|
if(test_metadata[@"reg"]) {
|
|
|
|
test_metadata = test_metadata[@"reg"][[NSString stringWithFormat:@"%c", [name characterAtIndex:first_dot.location+1]]];
|
|
|
|
}
|
|
|
|
|
2023-10-20 21:03:23 +00:00
|
|
|
int index = 0;
|
2023-10-06 02:27:52 +00:00
|
|
|
for(NSDictionary *test in [self testsInFile:file]) {
|
2023-10-16 19:40:24 +00:00
|
|
|
[self applyExecutionTest:test metadata:test_metadata];
|
2023-10-20 21:03:23 +00:00
|
|
|
++index;
|
2023-10-06 02:27:52 +00:00
|
|
|
}
|
2023-10-23 14:02:13 +00:00
|
|
|
|
|
|
|
if (execution_failures.size() != failures_before) {
|
|
|
|
[failures addObject:file];
|
|
|
|
}
|
2023-10-06 02:27:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 20:30:30 +00:00
|
|
|
// Lock in current failure rate.
|
2023-11-09 16:55:36 +00:00
|
|
|
XCTAssertLessThanOrEqual(execution_failures.size(), 0);
|
2023-10-07 18:28:44 +00:00
|
|
|
|
|
|
|
for(const auto &failure: execution_failures) {
|
2023-10-16 16:34:11 +00:00
|
|
|
NSLog(@"Failed %s — %s", failure.test_name.c_str(), failure.reason.c_str());
|
2023-10-07 18:28:44 +00:00
|
|
|
}
|
2023-11-09 16:55:36 +00:00
|
|
|
for(const auto &failure: permitted_failures) {
|
|
|
|
NSLog(@"Permitted failure of %s — %s", failure.test_name.c_str(), failure.reason.c_str());
|
|
|
|
}
|
2023-10-23 14:02:13 +00:00
|
|
|
|
2023-11-09 16:55:36 +00:00
|
|
|
NSLog(@"Files with failures, permitted or otherwise, were: %@", failures);
|
2023-10-05 18:37:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:53:38 +00:00
|
|
|
@end
|