mirror of
https://github.com/TomHarte/CLK.git
synced 2024-12-28 22:30:30 +00:00
Clean up Memory
.
This commit is contained in:
parent
e4fdf09149
commit
8d0deeb20e
@ -91,6 +91,7 @@ struct Registers {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
struct Memory {
|
struct Memory {
|
||||||
|
public:
|
||||||
using AccessType = InstructionSet::x86::AccessType;
|
using AccessType = InstructionSet::x86::AccessType;
|
||||||
|
|
||||||
template <typename IntT, AccessType type> struct ReturnType;
|
template <typename IntT, AccessType type> struct ReturnType;
|
||||||
@ -104,21 +105,12 @@ struct Memory {
|
|||||||
template <typename IntT> struct ReturnType<IntT, AccessType::ReadModifyWrite> { using type = IntT &; };
|
template <typename IntT> struct ReturnType<IntT, AccessType::ReadModifyWrite> { using type = IntT &; };
|
||||||
template <typename IntT> struct ReturnType<IntT, AccessType::PreauthorisedWrite> { using type = IntT &; };
|
template <typename IntT> struct ReturnType<IntT, AccessType::PreauthorisedWrite> { using type = IntT &; };
|
||||||
|
|
||||||
|
// Constructor.
|
||||||
enum class Tag {
|
|
||||||
Seeded,
|
|
||||||
AccessExpected,
|
|
||||||
Accessed,
|
|
||||||
};
|
|
||||||
|
|
||||||
std::unordered_map<uint32_t, Tag> tags;
|
|
||||||
std::vector<uint8_t> memory;
|
|
||||||
const Registers ®isters_;
|
|
||||||
|
|
||||||
Memory(Registers ®isters) : registers_(registers) {
|
Memory(Registers ®isters) : registers_(registers) {
|
||||||
memory.resize(1024*1024);
|
memory.resize(1024*1024);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialisation.
|
||||||
void clear() {
|
void clear() {
|
||||||
tags.clear();
|
tags.clear();
|
||||||
}
|
}
|
||||||
@ -132,60 +124,15 @@ struct Memory {
|
|||||||
tags[address] = Tag::AccessExpected;
|
tags[address] = Tag::AccessExpected;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t segment_base(InstructionSet::x86::Source segment) {
|
// Preauthorisation call-ins.
|
||||||
uint32_t physical_address;
|
|
||||||
using Source = InstructionSet::x86::Source;
|
|
||||||
switch(segment) {
|
|
||||||
default: physical_address = registers_.ds_; break;
|
|
||||||
case Source::ES: physical_address = registers_.es_; break;
|
|
||||||
case Source::CS: physical_address = registers_.cs_; break;
|
|
||||||
case Source::SS: physical_address = registers_.ss_; break;
|
|
||||||
}
|
|
||||||
return physical_address << 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
void preauthorise_stack_write(uint32_t) {}
|
void preauthorise_stack_write(uint32_t) {}
|
||||||
void preauthorise_stack_read(uint32_t) {}
|
void preauthorise_stack_read(uint32_t) {}
|
||||||
void preauthorise_read(InstructionSet::x86::Source, uint16_t, uint32_t) {}
|
void preauthorise_read(InstructionSet::x86::Source, uint16_t, uint32_t) {}
|
||||||
void preauthorise_read(uint16_t, uint32_t) {}
|
void preauthorise_read(uint16_t, uint32_t) {}
|
||||||
|
|
||||||
// Entry point used by the flow controller so that it can mark up locations at which the flags were written,
|
// Access call-ins.
|
||||||
// so that defined-flag-only masks can be applied while verifying RAM contents.
|
|
||||||
template <typename IntT, AccessType type>
|
|
||||||
typename ReturnType<IntT, type>::type &access(InstructionSet::x86::Source segment, uint16_t address, Tag tag) {
|
|
||||||
const uint32_t physical_address = (segment_base(segment) + address) & 0xf'ffff;
|
|
||||||
return access<IntT, type>(physical_address, tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
// An additional entry point for the flow controller; on the original 8086 interrupt vectors aren't relative
|
// Accesses an address based on segment:offset.
|
||||||
// to a selector, they're just at an absolute location.
|
|
||||||
template <typename IntT, AccessType type>
|
|
||||||
typename ReturnType<IntT, type>::type &access(uint32_t address, Tag tag) {
|
|
||||||
// Check for address wraparound
|
|
||||||
if(address >= 0x10'0001 - sizeof(IntT)) {
|
|
||||||
if constexpr (std::is_same_v<IntT, uint8_t>) {
|
|
||||||
address &= 0xf'ffff;
|
|
||||||
} else {
|
|
||||||
if(address == 0xf'ffff) {
|
|
||||||
// This is a 16-bit access comprising the final byte in memory and the first.
|
|
||||||
write_back_address_[0] = address;
|
|
||||||
write_back_address_[1] = 0;
|
|
||||||
write_back_value_ = memory[write_back_address_[0]] | (memory[write_back_address_[1]] << 8);
|
|
||||||
return write_back_value_;
|
|
||||||
} else {
|
|
||||||
address &= 0xf'ffff;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(tags.find(address) == tags.end()) {
|
|
||||||
printf("Access to unexpected RAM address");
|
|
||||||
}
|
|
||||||
tags[address] = tag;
|
|
||||||
return *reinterpret_cast<IntT *>(&memory[address]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Entry point for the 8086; simply notes that memory was accessed.
|
|
||||||
template <typename IntT, AccessType type>
|
template <typename IntT, AccessType type>
|
||||||
typename ReturnType<IntT, type>::type &access(InstructionSet::x86::Source segment, uint32_t address) {
|
typename ReturnType<IntT, type>::type &access(InstructionSet::x86::Source segment, uint32_t address) {
|
||||||
if constexpr (std::is_same_v<IntT, uint16_t>) {
|
if constexpr (std::is_same_v<IntT, uint16_t>) {
|
||||||
@ -208,6 +155,7 @@ struct Memory {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Accesses an address based on physical location.
|
||||||
template <typename IntT, AccessType type>
|
template <typename IntT, AccessType type>
|
||||||
typename ReturnType<IntT, type>::type &access(uint32_t address) {
|
typename ReturnType<IntT, type>::type &access(uint32_t address) {
|
||||||
return access<IntT, type>(address, Tag::Accessed);
|
return access<IntT, type>(address, Tag::Accessed);
|
||||||
@ -224,6 +172,65 @@ struct Memory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum class Tag {
|
||||||
|
Seeded,
|
||||||
|
AccessExpected,
|
||||||
|
Accessed,
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unordered_map<uint32_t, Tag> tags;
|
||||||
|
std::vector<uint8_t> memory;
|
||||||
|
const Registers ®isters_;
|
||||||
|
|
||||||
|
uint32_t segment_base(InstructionSet::x86::Source segment) {
|
||||||
|
uint32_t physical_address;
|
||||||
|
using Source = InstructionSet::x86::Source;
|
||||||
|
switch(segment) {
|
||||||
|
default: physical_address = registers_.ds_; break;
|
||||||
|
case Source::ES: physical_address = registers_.es_; break;
|
||||||
|
case Source::CS: physical_address = registers_.cs_; break;
|
||||||
|
case Source::SS: physical_address = registers_.ss_; break;
|
||||||
|
}
|
||||||
|
return physical_address << 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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>
|
||||||
|
typename ReturnType<IntT, type>::type &access(InstructionSet::x86::Source segment, uint16_t address, Tag tag) {
|
||||||
|
const uint32_t physical_address = (segment_base(segment) + address) & 0xf'ffff;
|
||||||
|
return access<IntT, type>(physical_address, tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
// An additional entry point for the flow controller; on the original 8086 interrupt vectors aren't relative
|
||||||
|
// to a selector, they're just at an absolute location.
|
||||||
|
template <typename IntT, AccessType type>
|
||||||
|
typename ReturnType<IntT, type>::type &access(uint32_t address, Tag tag) {
|
||||||
|
// Check for address wraparound
|
||||||
|
if(address > 0x10'0000 - sizeof(IntT)) {
|
||||||
|
if constexpr (std::is_same_v<IntT, uint8_t>) {
|
||||||
|
address &= 0xf'ffff;
|
||||||
|
} else {
|
||||||
|
address &= 0xf'ffff;
|
||||||
|
if(address == 0xf'ffff) {
|
||||||
|
// This is a 16-bit access comprising the final byte in memory and the first.
|
||||||
|
write_back_address_[0] = address;
|
||||||
|
write_back_address_[1] = 0;
|
||||||
|
write_back_value_ = memory[write_back_address_[0]] | (memory[write_back_address_[1]] << 8);
|
||||||
|
return write_back_value_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(tags.find(address) == tags.end()) {
|
||||||
|
printf("Access to unexpected RAM address");
|
||||||
|
}
|
||||||
|
tags[address] = tag;
|
||||||
|
return *reinterpret_cast<IntT *>(&memory[address]);
|
||||||
|
}
|
||||||
|
|
||||||
static constexpr uint32_t NoWriteBack = 0; // A low byte address of 0 can't require write-back.
|
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};
|
uint32_t write_back_address_[2] = {NoWriteBack, NoWriteBack};
|
||||||
uint16_t write_back_value_;
|
uint16_t write_back_value_;
|
||||||
@ -524,8 +531,9 @@ struct FailedExecution {
|
|||||||
int mask_position = 0;
|
int mask_position = 0;
|
||||||
for(NSArray<NSNumber *> *ram in final_state[@"ram"]) {
|
for(NSArray<NSNumber *> *ram in final_state[@"ram"]) {
|
||||||
const uint32_t address = [ram[0] intValue];
|
const uint32_t address = [ram[0] intValue];
|
||||||
|
const auto value = execution_support.memory.access<uint8_t, Memory::AccessType::Read>(address);
|
||||||
|
|
||||||
if((mask_position != 1) && execution_support.memory.memory[address] == [ram[1] intValue]) {
|
if((mask_position != 1) && value == [ram[1] intValue]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -535,7 +543,7 @@ struct FailedExecution {
|
|||||||
while(mask_position < 2) {
|
while(mask_position < 2) {
|
||||||
const uint8_t mask = mask_position ? (flags_mask >> 8) : (flags_mask & 0xff);
|
const uint8_t mask = mask_position ? (flags_mask >> 8) : (flags_mask & 0xff);
|
||||||
++mask_position;
|
++mask_position;
|
||||||
if((execution_support.memory.memory[address] & mask) == ([ram[1] intValue] & mask)) {
|
if((value & mask) == ([ram[1] intValue] & mask)) {
|
||||||
matched_with_mask = true;
|
matched_with_mask = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user