2020-10-26 01:10:04 +00:00
|
|
|
//
|
|
|
|
// MemoryMap.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 25/10/2020.
|
|
|
|
// Copyright © 2020 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2020-10-26 01:10:04 +00:00
|
|
|
|
|
|
|
#include <array>
|
2021-02-28 03:13:49 +00:00
|
|
|
#include <bitset>
|
2024-01-04 16:21:32 +00:00
|
|
|
#include <cassert>
|
2020-10-26 01:10:04 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "../AppleII/LanguageCardSwitches.hpp"
|
|
|
|
#include "../AppleII/AuxiliaryMemorySwitches.hpp"
|
|
|
|
|
2023-05-10 21:02:18 +00:00
|
|
|
namespace Apple::IIgs {
|
2020-10-26 01:10:04 +00:00
|
|
|
|
|
|
|
class MemoryMap {
|
|
|
|
public:
|
2020-10-27 00:30:41 +00:00
|
|
|
// MARK: - Initial construction and configuration.
|
|
|
|
|
2022-09-13 20:31:06 +00:00
|
|
|
MemoryMap(bool is_rom03) : auxiliary_switches_(*this), language_card_(*this) {
|
|
|
|
setup_shadow_maps(is_rom03);
|
2022-09-13 20:02:49 +00:00
|
|
|
}
|
2020-10-26 01:10:04 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
/// Sets the ROM and RAM storage underlying this MemoryMap.
|
|
|
|
void set_storage(std::vector<uint8_t> &ram, std::vector<uint8_t> &rom);
|
2020-10-26 01:10:04 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
// MARK: - Live bus access notifications and register access.
|
2020-10-26 01:10:04 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
void set_shadow_register(uint8_t value);
|
|
|
|
uint8_t get_shadow_register() const;
|
2020-10-26 01:10:04 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
void set_speed_register(uint8_t value);
|
2020-10-26 01:10:04 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
void set_state_register(uint8_t value);
|
|
|
|
uint8_t get_state_register() const;
|
2021-02-28 20:14:32 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
void access(uint16_t address, bool is_read);
|
2020-10-27 23:33:47 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
using AuxiliaryMemorySwitches = Apple::II::AuxiliaryMemorySwitches<MemoryMap>;
|
|
|
|
const AuxiliaryMemorySwitches &auxiliary_switches() const {
|
|
|
|
return auxiliary_switches_;
|
2020-10-26 01:10:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
using LanguageCardSwitches = Apple::II::LanguageCardSwitches<MemoryMap>;
|
|
|
|
const LanguageCardSwitches &language_card_switches() const {
|
|
|
|
return language_card_;
|
2020-10-27 00:30:41 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
// MARK: - Accessors for reading and writing RAM.
|
2020-10-29 01:58:20 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
struct Region {
|
|
|
|
uint8_t *write = nullptr;
|
|
|
|
const uint8_t *read = nullptr;
|
|
|
|
uint8_t flags = 0;
|
2021-02-28 20:14:32 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
enum Flag: uint8_t {
|
|
|
|
Is1Mhz = 1 << 0, // Both reads and writes should be synchronised with the 1Mhz clock.
|
|
|
|
IsIO = 1 << 1, // Indicates that this region should be checked for soft switches, registers, etc.
|
|
|
|
};
|
|
|
|
};
|
2020-10-27 00:30:41 +00:00
|
|
|
|
2024-01-04 15:36:33 +00:00
|
|
|
const Region ®ion(uint32_t address) const { return regions_[region_map_[address >> 8]]; }
|
2024-01-04 15:29:38 +00:00
|
|
|
uint8_t read(const Region ®ion, uint32_t address) const {
|
2024-01-04 15:25:27 +00:00
|
|
|
return region.read ? region.read[address] : 0xff;
|
2020-10-29 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
bool is_shadowed(const Region ®ion, uint32_t address) const {
|
2024-01-04 16:17:20 +00:00
|
|
|
// ROM is never shadowed.
|
|
|
|
if(!region.write) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
const auto physical = physical_address(region, address);
|
2024-01-04 16:11:26 +00:00
|
|
|
assert(physical >= 0 && physical <= 0xff'ffff);
|
2024-01-04 15:36:33 +00:00
|
|
|
return shadow_pages_[(physical >> 10) & 127] & shadow_banks_[physical >> 17];
|
2020-10-29 01:58:20 +00:00
|
|
|
}
|
2024-01-04 15:25:27 +00:00
|
|
|
void write(const Region ®ion, uint32_t address, uint8_t value) {
|
|
|
|
if(!region.write) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-29 01:58:20 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
// Write once.
|
|
|
|
region.write[address] = value;
|
2020-10-29 01:58:20 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
// Write again, either to the same place (if unshadowed) or to the shadow destination.
|
|
|
|
static constexpr std::size_t shadow_mask[2] = {0xff'ffff, 0x01'ffff};
|
|
|
|
const bool shadowed = is_shadowed(region, address);
|
2024-01-04 15:36:33 +00:00
|
|
|
shadow_base_[shadowed][physical_address(region, address) & shadow_mask[shadowed]] = value;
|
2020-10-29 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
// The objective is to support shadowing:
|
|
|
|
// 1. without storing a whole extra pointer, and such that the shadowing flags
|
|
|
|
// are orthogonal to the current auxiliary memory settings;
|
|
|
|
// 2. in such a way as to support shadowing both in banks $00/$01 and elsewhere; and
|
|
|
|
// 3. to do so without introducing too much in the way of branching.
|
|
|
|
//
|
|
|
|
// Hence the implemented solution: if shadowing is enabled then use the distance from the start of
|
|
|
|
// physical RAM modulo 128k indexed into the bank $e0/$e1 RAM.
|
|
|
|
//
|
|
|
|
// With a further twist: the modulo and pointer are indexed on ::IsShadowed to eliminate a branch
|
|
|
|
// even on that.
|
2020-10-28 02:09:45 +00:00
|
|
|
|
2020-10-27 00:30:41 +00:00
|
|
|
private:
|
2020-10-29 01:58:20 +00:00
|
|
|
AuxiliaryMemorySwitches auxiliary_switches_;
|
|
|
|
LanguageCardSwitches language_card_;
|
|
|
|
friend AuxiliaryMemorySwitches;
|
|
|
|
friend LanguageCardSwitches;
|
2020-10-27 00:30:41 +00:00
|
|
|
|
2021-09-10 02:06:36 +00:00
|
|
|
uint8_t shadow_register_ = 0x00;
|
2020-10-27 00:30:41 +00:00
|
|
|
uint8_t speed_register_ = 0x00;
|
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
// MARK: - Banking.
|
2024-01-03 16:40:04 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
void assert_is_region(uint8_t start, uint8_t end);
|
|
|
|
template <int type> void set_paging();
|
2020-10-27 00:30:41 +00:00
|
|
|
|
2024-01-04 15:36:33 +00:00
|
|
|
uint8_t *ram_base_ = nullptr;
|
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
// Memory layout here is done via double indirection; the main loop should:
|
|
|
|
// (i) use the top two bytes of the address to get an index from region_map; and
|
|
|
|
// (ii) use that to index the memory_regions table.
|
2020-11-30 23:07:03 +00:00
|
|
|
//
|
2024-01-04 15:25:27 +00:00
|
|
|
// Pointers are eight bytes at the time of writing, so the extra level of indirection
|
|
|
|
// reduces what would otherwise be a 1.25mb table down to not a great deal more than 64kb.
|
2024-01-04 15:36:33 +00:00
|
|
|
std::array<uint8_t, 65536> region_map_{};
|
|
|
|
std::array<Region, 40> regions_; // An assert above ensures that this is large enough; there's no
|
|
|
|
// doctrinal reason for it to be whatever size it is now, just
|
|
|
|
// adjust as required.
|
2020-11-17 00:54:12 +00:00
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
std::size_t physical_address(const Region ®ion, uint32_t address) const {
|
2024-01-04 15:36:33 +00:00
|
|
|
return std::size_t(®ion.write[address] - ram_base_);
|
2020-10-27 01:49:47 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 15:25:27 +00:00
|
|
|
// MARK: - Shadowing
|
2021-02-28 03:13:10 +00:00
|
|
|
|
2022-09-13 20:02:49 +00:00
|
|
|
// Various precomputed bitsets describing key regions; std::bitset doesn't support constexpr instantiation
|
|
|
|
// beyond the first 64 bits at the time of writing, alas, so these are generated at runtime.
|
2024-01-04 15:36:33 +00:00
|
|
|
std::bitset<128> shadow_text1_;
|
|
|
|
std::bitset<128> shadow_text2_;
|
|
|
|
std::bitset<128> shadow_highres1_, shadow_highres1_aux_;
|
|
|
|
std::bitset<128> shadow_highres2_, shadow_highres2_aux_;
|
|
|
|
std::bitset<128> shadow_superhighres_;
|
2024-01-04 15:25:27 +00:00
|
|
|
void setup_shadow_maps(bool is_rom03);
|
|
|
|
void set_shadowing();
|
2022-09-13 20:02:49 +00:00
|
|
|
|
2024-01-04 15:36:33 +00:00
|
|
|
uint8_t *shadow_base_[2] = {nullptr, nullptr};
|
2021-02-28 03:13:10 +00:00
|
|
|
|
2024-01-04 15:36:33 +00:00
|
|
|
// Divide the final 128kb of memory into 1kb chunks and flag to indicate whether
|
2021-02-28 20:14:32 +00:00
|
|
|
// each is a potential destination for shadowing.
|
2024-01-04 15:36:33 +00:00
|
|
|
std::bitset<128> shadow_pages_{};
|
|
|
|
|
|
|
|
// Divide the whole 16mb of memory into 128kb chunks and flag to indicate whether
|
2021-02-28 20:14:32 +00:00
|
|
|
// each is a potential source of shadowing.
|
2024-01-04 15:36:33 +00:00
|
|
|
std::bitset<128> shadow_banks_{};
|
2024-01-03 18:21:39 +00:00
|
|
|
};
|
2020-10-28 01:56:03 +00:00
|
|
|
|
2020-10-26 01:10:04 +00:00
|
|
|
}
|