diff --git a/devices/memctrl/psx.cpp b/devices/memctrl/psx.cpp new file mode 100644 index 0000000..1b8d40d --- /dev/null +++ b/devices/memctrl/psx.cpp @@ -0,0 +1,128 @@ +/* +DingusPPC - The Experimental PowerPC Macintosh emulator +Copyright (C) 2018-22 divingkatae and maximum + (theweirdo) spatium + +(Contact divingkatae#1017 or powermax#2286 on Discord for more info) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/** PSX Memory Controller emulation. */ + +#include +#include +#include + +#include +#include + +PsxCtrl::PsxCtrl(int bridge_num, std::string name) +{ + supports_types(HWCompType::MEM_CTRL | HWCompType::MMIO_DEV); + + // add MMIO region for the PSX control registers + add_mmio_region(0xF8000000, 0x70, this); + + this->sys_id = 0x30040000; // TODO: use correct value here! + this->chip_rev = 0; // old PSX, what's about PSX+? + this->sys_config = PSX_BUS_SPEED_50; +} + +uint32_t PsxCtrl::read(uint32_t rgn_start, uint32_t offset, int size) +{ + switch (offset >> 3) { + case PsxReg::Sys_Id: + return this->sys_id; + case PsxReg::Revision: + return this->chip_rev; + case PsxReg::Sys_Config: + return this->sys_config; + default: + LOG_F(WARNING, "PSX: read from unsupported control register at 0x%X", offset); + return 0; + } +} + +void PsxCtrl::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size) +{ + switch (offset >> 3) { + case PsxReg::Sys_Config: + this->sys_config = value; + break; + case PsxReg::ROM_Config: + this->rom_cfg = value; + break; + case PsxReg::DRAM_Config: + this->dram_cfg = value; + break; + case PsxReg::DRAM_Refresh: + this->dram_refresh = value; + break; + case PsxReg::Flash_Config: + this->flash_cfg = value; + break; + case PsxReg::Page1_Mapping: + case PsxReg::Page2_Mapping: + case PsxReg::Page3_Mapping: + case PsxReg::Page4_Mapping: + case PsxReg::Page5_Mapping: + this->pages_cfg[(offset >> 3) - PsxReg::Page1_Mapping] = value; + break; + default: + LOG_F(WARNING, "PSX: write to unsupported/read-only control register at 0x%X", offset); + }; +} + +void PsxCtrl::insert_ram_dimm(int slot_num, uint32_t capacity) +{ + if (slot_num < 0 || slot_num >= 5) { + ABORT_F("PSX: invalid DIMM slot %d", slot_num); + } + + switch (capacity) { + case DRAM_CAP_2MB: + case DRAM_CAP_4MB: + case DRAM_CAP_8MB: + case DRAM_CAP_16MB: + case DRAM_CAP_32MB: + this->bank_sizes[slot_num] = capacity; + break; + default: + ABORT_F("PSX: unsupported DRAM capacity %d", capacity); + } +} + +void PsxCtrl::map_phys_ram() +{ + uint32_t total_ram = 0; + + for (int i = 0; i < 5; i++) { + total_ram += this->bank_sizes[i]; + } + + if (total_ram > DRAM_CAP_32MB) { + ABORT_F("PSX: RAM bigger than 32MB not supported yet"); + } + + if (!add_ram_region(0x00000000, total_ram)) { + ABORT_F("PSX: could not allocate RAM storage"); + } +} + +static const DeviceDescription Psx_Descriptor = { + PsxCtrl::create, {}, {} +}; + +REGISTER_DEVICE(Psx, Psx_Descriptor); diff --git a/devices/memctrl/psx.h b/devices/memctrl/psx.h new file mode 100644 index 0000000..c67d02e --- /dev/null +++ b/devices/memctrl/psx.h @@ -0,0 +1,88 @@ +/* +DingusPPC - The Experimental PowerPC Macintosh emulator +Copyright (C) 2018-22 divingkatae and maximum + (theweirdo) spatium + +(Contact divingkatae#1017 or powermax#2286 on Discord for more info) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/** PSX Memory Controller definitions. */ + +#ifndef PSX_MEMCTRL_H +#define PSX_MEMCTRL_H + +#include +#include + +#include +#include +#include + +namespace PsxReg { + enum { + Sys_Id = 0, + Revision = 1, + Sys_Config = 2, + ROM_Config = 3, + DRAM_Config = 4, + DRAM_Refresh = 5, + Flash_Config = 6, + Page1_Mapping = 8, + Page2_Mapping = 9, + Page3_Mapping = 10, + Page4_Mapping = 11, + Page5_Mapping = 12, + Bus_Timeout = 13 + }; +}; // namespace PsxReg + +/** Bus (aka CPU) speed constants. */ +enum { + PSX_BUS_SPEED_38 = 0, // bus frequency 38 MHz + PSX_BUS_SPEED_33 = 1, // bus frequency 33 MHz + PSX_BUS_SPEED_40 = 2, // bus frequency 40 MHz + PSX_BUS_SPEED_50 = 3, // bus frequency 50 MHz +}; + +class PsxCtrl : public MemCtrlBase, public MMIODevice { +public: + PsxCtrl(int bridge_num, std::string name); + ~PsxCtrl() = default; + + static std::unique_ptr create() { + return std::unique_ptr(new PsxCtrl(1, "PSX-PCI1")); + }; + + // MMIODevice methods + uint32_t read(uint32_t rgn_start, uint32_t offset, int size); + void write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size); + + void insert_ram_dimm(int slot_num, uint32_t capacity); + void map_phys_ram(); + +private: + uint32_t sys_id; + uint32_t chip_rev; + uint32_t sys_config; + uint32_t rom_cfg; + uint32_t dram_cfg; + uint32_t dram_refresh; + uint32_t flash_cfg; + uint32_t pages_cfg[5]; + uint32_t bank_sizes[5] = {}; +}; + +#endif // PSX_MEMCTRL_H