30 , flash(getName() +
" flash", AmdFlashChip::M29W640GB, {}, config)
31 , ram(config, getName() +
" ram",
"ram", 2048 * 1024)
32 , eeprom(getName() +
" eeprom",
37 , ym2413(getName() +
" ym2413", config)
46 configRegs[0x35] = 0xf0;
47 getCPUInterface().register_IO_Out(idControlPort(),
this);
48 getCPUInterface().register_IO_In (idControlPort(),
this);
62 writeSndLVL(0x1b, time);
63 writePSGCtrl(0x1b, time);
75 configRegs[0x00] = 0x30;
76 for (
int i : {0x01, 0x02, 0x03}) configRegs[i] = 0;
77 configRegs[0x05] = shadowConfigRegs[0x05] = 0;
79 configRegs[0x06] = shadowConfigRegs[0x06] = 0xF8;
80 configRegs[0x07] = shadowConfigRegs[0x07] = 0x50;
81 configRegs[0x08] = shadowConfigRegs[0x08] = 0x00;
82 configRegs[0x09] = shadowConfigRegs[0x09] = 0x85;
83 configRegs[0x0a] = shadowConfigRegs[0x0a] = 0x03;
84 configRegs[0x0b] = shadowConfigRegs[0x0b] = 0x40;
86 for (
int i : {0x0f, 0x15, 0x1b}) {
87 configRegs[i] = shadowConfigRegs[i] = 0;
90 configRegs[0x1e] = shadowConfigRegs[0x1e] = 0xff;
91 configRegs[0x20] = 0x02;
92 configRegs[0x28] = 0b11'10'01'00;
96 writeCfgEEPR(0, time);
109 ideSelectedDevice = 0;
110 ideSoftReset =
false;
111 ideDevices[0]->reset(time);
112 ideDevices[1]->reset(time);
130 if (!delayedConfig())
return;
132 if ((!delayedConfig4000() && (address == 0x0000) && (
getCPU().isM1Cycle(address))) ||
133 ( delayedConfig4000() && (address <= 0x4000) && (address < 0x4010))) {
135 for (
auto i :
xrange(0x05, 0x1f)) {
136 configRegs[i] = shadowConfigRegs[i];
141Carnivore2::SubDevice Carnivore2::getSubDevice(
word address)
const
145 if (slotExpanded()) {
146 auto page = narrow<byte>(address >> 14);
147 byte selectedSubSlot = (subSlotReg >> (2 * page)) & 0x03;
148 if (subSlotEnabled(selectedSubSlot)) {
149 subSlot = selectedSubSlot;
153 if (subSlotEnabled(i)) {
160 using enum SubDevice;
161 if (subSlot == (configRegs[0x28] & 0b00'00'00'11) >> 0) {
163 }
else if (subSlot == (configRegs[0x28] & 0b00'00'11'00) >> 2) {
165 }
else if (subSlot == (configRegs[0x28] & 0b00'11'00'00) >> 4) {
167 }
else if (subSlot == (configRegs[0x28] & 0b11'00'00'00) >> 6) {
176 if (slotExpanded() && (address == 0xffff)) {
177 return subSlotReg ^ 0xff;
179 switch (getSubDevice(address)) {
180 using enum SubDevice;
181 case MultiMapper:
return readMultiMapperSlot(address, time);
182 case IDE:
return readIDESlot(address, time);
183 case MemoryMapper:
return readMemoryMapperSlot(address);
184 case FmPac:
return readFmPacSlot(address);
185 default:
return 0xff;
191 if (slotExpanded() && (address == 0xffff)) {
192 return subSlotReg ^ 0xff;
194 switch (getSubDevice(address)) {
195 using enum SubDevice;
196 case MultiMapper:
return peekMultiMapperSlot(address, time);
197 case IDE:
return peekIDESlot(address, time);
198 case MemoryMapper:
return peekMemoryMapperSlot(address);
199 case FmPac:
return peekFmPacSlot(address);
200 default:
return 0xff;
206 if (slotExpanded() && (address == 0xffff)) {
211 switch (getSubDevice(address)) {
212 using enum SubDevice;
214 writeMultiMapperSlot(address, value, time);
217 writeIDESlot(address, value, time);
220 writeMemoryMapperSlot(address, value);
223 writeFmPacSlot(address, value, time);
231unsigned Carnivore2::getDirectFlashAddr()
const
233 return (configRegs[0x01] << 0) |
234 (configRegs[0x02] << 8) |
235 (configRegs[0x03] << 16);
238byte Carnivore2::peekConfigRegister(
word address, EmuTime::param time)
const
241 if ((0x05 <= address) && (address <= 0x1e)) {
244 return shadowConfigRegs[address];
247 case 0x04:
return flash.
peek(getDirectFlashAddr());
248 case 0x1f:
return configRegs[0x00];
249 case 0x23:
return byte(configRegs[address] |
251 case 0x2C:
return '2';
252 case 0x2D:
return '5';
253 case 0x2E:
return '0';
254 default:
return configRegs[address];
259byte Carnivore2::readConfigRegister(
word address, EmuTime::param time)
262 if (address == 0x04) {
263 return flash.
read(getDirectFlashAddr());
265 return peekConfigRegister(address, time);
269static constexpr float volumeLevel(
byte volume)
271 constexpr std::array<byte, 8> tab = {5, 6, 7, 8, 10, 12, 14, 16};
272 return narrow<float>(tab[volume & 7]) * (1.0f / 16.0f);
275void Carnivore2::writeSndLVL(
byte value, EmuTime::param time)
277 configRegs[0x22] = value;
282void Carnivore2::writeCfgEEPR(
byte value, EmuTime::param time)
284 configRegs[0x23] = value & 0x0e;
290void Carnivore2::writePSGCtrl(
byte value, EmuTime::param time)
293 if ((value ^ configRegs[0x24]) & 0x80) {
294 byte ioBase = (configRegs[0x30] & 0x01) ? 0x10 : 0xa0;
303 configRegs[0x24] = value;
307void Carnivore2::writePSGAlt(
byte value)
309 if ((value ^ configRegs[0x30]) & 0x01) {
310 if (configRegs[0x24] & 0x80) {
311 byte ioBaseOld = (configRegs[0x30] & 0x01) ? 0x10 : 0xa0;
312 byte ioBaseNew = (value & 0x01) ? 0x10 : 0xa0;
319 configRegs[0x30] = value;
322void Carnivore2::writePFXN(
byte value)
324 byte oldPort = idControlPort();
325 configRegs[0x35] = 0xf0 | (value & 0b11);
326 if (
auto newPort = idControlPort(); newPort != oldPort) {
335[[nodiscard]]
static bool bitPairsUnique(uint8_t x)
338 for (
int i = 0; i < 4; ++i) {
339 seen |= 1 << (x & 3);
342 return seen == 0b1111;
345void Carnivore2::writeConfigRegister(
word address,
byte value, EmuTime::param time)
348 if ((0x05 <= address) && (address <= 0x1e)) {
350 if (address == 0x05) value &= 0x7f;
351 if ((address == 0x1e) && ((value & 0x8f) == 0x0f))
return;
353 shadowConfigRegs[address] = value;
354 if (!delayedConfig()) configRegs[address] = value;
357 case 0x03: configRegs[address] = value & 0x7f;
break;
358 case 0x04: flash.
write(getDirectFlashAddr(), value);
break;
359 case 0x1f: configRegs[0x00] = value;
break;
360 case 0x20: configRegs[address] = value & 0x07;
break;
361 case 0x22: writeSndLVL(value, time);
break;
362 case 0x23: writeCfgEEPR(value, time);
break;
363 case 0x24: writePSGCtrl(value, time);
break;
364 case 0x30: writePSGAlt(value);
break;
365 case 0x35: writePFXN(value);
break;
367 if (!bitPairsUnique(value)) {
369 "Illegal value of ", value,
370 "written to SLM_cfg register");
373 default: configRegs[address] = value;
break;
378bool Carnivore2::isConfigReg(
word address)
const
380 if (configRegs[0x00] & 0x80)
return false;
381 unsigned base = ((configRegs[0x00] & 0x60) << 9) | 0xF80;
382 return (base <= address) && (address < (base + 0x40));
385std::pair<unsigned, byte> Carnivore2::decodeMultiMapper(
word address)
const
388 for (
auto i :
xrange(4)) {
389 auto base = subspan<6>(configRegs, (i * 6) + 6);
391 if (mult & 8)
continue;
393 byte sizeCode = mult & 7;
394 if (sizeCode < 3)
continue;
397 bool mirroringDisabled = mult & 0x40;
398 static constexpr std::array checkMasks = {
399 std::array<byte, 8>{0x00, 0x00, 0x00, 0x30, 0x60, 0xc0, 0x80, 0x00},
400 std::array<byte, 8>{0x00, 0x00, 0x00, 0xf0, 0xe0, 0xc0, 0x80, 0x00},
402 byte checkMask = checkMasks[mirroringDisabled][sizeCode];
403 if (((address >> 8) & checkMask) != (base[5] & checkMask))
continue;
406 byte bank = base[2] & base[4];
407 unsigned size = 512 << sizeCode;
408 unsigned addr = (bank *
size) | (address & (size - 1));
409 addr += configRegs[0x05] * 0x10000;
413 return {unsigned(-1),
byte(-1)};
416bool Carnivore2::sccAccess(
word address)
const
418 if (!sccEnabled())
return false;
419 if (sccMode & 0x20) {
421 return (0xb800 <= address) && (address < 0xc000) &&
422 ((sccBank[3] & 0x80) == 0x80);
425 return (0x9800 <= address) && (address < 0xa000) &&
426 ((sccBank[2] & 0x3f) == 0x3f);
430byte Carnivore2::readMultiMapperSlot(
word address, EmuTime::param time)
432 if (isConfigReg(address)) {
433 return readConfigRegister(address, time);
435 if (sccAccess(address)) {
436 return scc.
readMem(narrow_cast<uint8_t>(address & 0xff), time);
439 auto [addr, mult] = decodeMultiMapper(address);
440 if (addr ==
unsigned(-1))
return 0xff;
443 return ram[addr & 0x1fffff];
445 return flash.
read(addr);
449byte Carnivore2::peekMultiMapperSlot(
word address, EmuTime::param time)
const
451 if (isConfigReg(address)) {
452 return peekConfigRegister(address, time);
455 auto [addr, mult] = decodeMultiMapper(address);
456 if (addr ==
unsigned(-1))
return 0xff;
459 return ram[addr & 0x1fffff];
461 return flash.
peek(addr);
465void Carnivore2::writeMultiMapperSlot(
word address,
byte value, EmuTime::param time)
467 if (isConfigReg(address)) {
469 return writeConfigRegister(address, value, time);
473 for (
auto i :
xrange(4)) {
474 auto base = subspan<6>(configRegs, (i * 6) + 6);
479 if (((address >> 8) & mask) == (addr & mask)) {
481 configRegs[(i * 6) + 6 + 2] = value;
482 shadowConfigRegs[(i * 6) + 6 + 2] = value;
487 auto [addr, mult] = decodeMultiMapper(address);
488 if ((addr !=
unsigned(-1)) && (mult & 0x10)) {
490 ram[addr & 0x1fffff] = value;
492 flash.
write(addr, value);
496 if (sccEnabled() && ((address | 1) == 0xbfff)) {
501 if (((sccMode & 0x10) == 0x00) &&
502 ((address & 0x1800) == 0x1000)) {
503 auto region = narrow<byte>((address >> 13) - 2);
504 sccBank[region] = value;
505 }
else if (sccAccess(address)) {
506 scc.
writeMem(narrow_cast<uint8_t>(address & 0xff), value, time);
510byte Carnivore2::readIDESlot(
word address, EmuTime::param time)
513 if (ideRegsEnabled() && ((address & 0xfe00) == 0x7c00)) {
515 switch (address & 1) {
517 auto tmp = ideReadData(time);
518 ideRead = narrow_cast<byte>(tmp >> 8);
519 return narrow_cast<byte>(tmp & 0xff);
525 if (ideRegsEnabled() && ((address & 0xff00) == 0x7e00)) {
527 return ideReadReg(address & 0xf, time);
529 if ((0x4000 <= address) && (address < 0x8000)) {
531 unsigned addr = (address & 0x3fff) + (ideBank() * 0x4000) + 0x10000;
532 if (readBIOSfromRAM()) {
535 return flash.
read(addr);
541byte Carnivore2::peekIDESlot(
word address, EmuTime::param )
const
543 if (ideRegsEnabled() && ((address & 0xfe00) == 0x7c00)) {
547 if (ideRegsEnabled() && ((address & 0xff00) == 0x7e00)) {
551 if ((0x4000 <= address) && (address < 0x8000)) {
553 unsigned addr = (address & 0x3fff) + (ideBank() * 0x4000) + 0x10000;
554 if (readBIOSfromRAM()) {
557 return flash.
peek(addr);
563void Carnivore2::writeIDESlot(
word address,
byte value, EmuTime::param time)
566 if (address == 0x4104) {
567 ideControlReg = value;
569 }
else if (ideRegsEnabled() && ((address & 0xfe00) == 0x7c00)) {
571 switch (address & 1) {
576 auto tmp =
word((value << 8) | ideWrite);
577 ideWriteData(tmp, time);
582 }
else if (ideRegsEnabled() && ((address & 0xff00) == 0x7e00)) {
584 ideWriteReg(address & 0xf, value, time);
588word Carnivore2::ideReadData(EmuTime::param time)
590 return ideDevices[ideSelectedDevice]->readData(time);
593void Carnivore2::ideWriteData(
word value, EmuTime::param time)
595 ideDevices[ideSelectedDevice]->writeData(value, time);
598byte Carnivore2::ideReadReg(
byte reg, EmuTime::param time)
600 if (reg == 14) reg = 7;
610 return narrow_cast<byte>(ideReadData(time) & 0xff);
612 auto result = ideDevices[ideSelectedDevice]->readReg(reg, time);
614 result = (result & 0xef) | (ideSelectedDevice ? 0x10 : 0x00);
621void Carnivore2::ideWriteReg(
byte reg,
byte value, EmuTime::param time)
624 if ((reg == 14) && !(value & 0x04)) {
626 ideSoftReset =
false;
631 ideWriteData(narrow_cast<word>((value << 8) | value), time);
633 if ((reg == 14) && (value & 0x04)) {
636 ideDevices[0]->reset(time);
637 ideDevices[1]->reset(time);
640 ideSelectedDevice = (value & 0x10) ? 1 : 0;
642 ideDevices[ideSelectedDevice]->writeReg(reg, value, time);
648bool Carnivore2::isMemMapControl(
word address)
const
650 return (port3C & 0x80) &&
651 (( (port3C & 0x08) && ((address & 0xc000) == 0x4000)) ||
652 (!(port3C & 0x08) && ((address & 0xc000) == 0x8000)));
655unsigned Carnivore2::getMemoryMapperAddress(
word address)
const
657 return (address & 0x3fff) +
658 0x4000 * memMapRegs[address >> 14] +
662bool Carnivore2::isMemoryMapperWriteProtected(
word address)
const
664 auto page = address >> 14;
665 return (port3C & (1 << page)) != 0;
668byte Carnivore2::peekMemoryMapperSlot(
word address)
const
670 if (isMemMapControl(address)) {
671 switch (address & 0xff) {
674 case 0xfc:
case 0xfd:
case 0xfe:
case 0xff:
675 return memMapRegs[address & 0x03];
678 return ram[getMemoryMapperAddress(address)];
681byte Carnivore2::readMemoryMapperSlot(
word address)
const
683 return peekMemoryMapperSlot(address);
686void Carnivore2::writeMemoryMapperSlot(
word address,
byte value)
688 if (isMemMapControl(address)) {
689 switch (address & 0xff) {
691 value |= (value & 0x02) << 6;
694 case 0xfc:
case 0xfd:
case 0xfe:
case 0xff:
695 memMapRegs[address & 0x03] = value & 0x3f;
699 if (!isMemoryMapperWriteProtected(address)) {
700 ram[getMemoryMapperAddress(address)] = value;
704byte Carnivore2::readFmPacSlot(
word address)
706 if (address == 0x7ff6) {
708 }
else if (address == 0x7ff7) {
710 }
else if ((0x4000 <= address) && (address < 0x8000)) {
711 if (fmPacSramEnabled()) {
712 if (address < 0x5ffe) {
713 return ram[(address & 0x1fff) | 0xfe000];
714 }
else if (address == 0x5ffe) {
716 }
else if (address == 0x5fff) {
722 unsigned addr = (address & 0x3fff) + (0x4000 * fmPacBank) + 0x30000;
723 if (readBIOSfromRAM()) {
726 return flash.
read(addr);
733byte Carnivore2::peekFmPacSlot(
word address)
const
735 if (address == 0x7ff6) {
737 }
else if (address == 0x7ff7) {
739 }
else if ((0x4000 <= address) && (address < 0x8000)) {
740 if (fmPacSramEnabled()) {
741 if (address < 0x5ffe) {
742 return ram[(address & 0x1fff) | 0xfe000];
743 }
else if (address == 0x5ffe) {
745 }
else if (address == 0x5fff) {
751 unsigned addr = (address & 0x3fff) + (0x4000 * fmPacBank) + 0x30000;
752 if (readBIOSfromRAM()) {
755 return flash.
peek(addr);
762void Carnivore2::writeFmPacSlot(
word address,
byte value, EmuTime::param time)
764 if ((0x4000 <= address) && (address < 0x5ffe)) {
765 if (fmPacSramEnabled()) {
766 ram[(address & 0x1fff) | 0xfe000] = value;
768 }
else if (address == 0x5ffe) {
770 }
else if (address == 0x5fff) {
772 }
else if (address ==
one_of(0x7ff4, 0x7ff5)) {
773 ym2413.
writePort(address & 1, value, time);
774 }
else if (address == 0x7ff6) {
775 fmPacEnable = value & 0x11;
776 }
else if (address == 0x7ff7) {
777 fmPacBank = value & 0x03;
783 return peekIO(port, time);
789 if (memMapReadEnabled() && ((port & 0xfc) == 0xfc)) {
792 }
else if ((port & 0xff) == idControlPort() && PF0_RV != 0) {
795 }
else if (PF0_RV == 2) {
805 if (((port & 0xff) == 0xa0) || ((port & 0xff) == 0x10)) {
806 psgLatch = value & 0x0f;
807 }
else if ((port & 0xff) == 0xa1 || (port & 0xff) == 0x11) {
809 }
else if (((port & 0xfe) == 0x7c) &&
810 (fmPacPortEnabled1() || fmPacPortEnabled2())) {
813 }
else if (((port & 0xff) == 0x3c) && writePort3cEnabled()) {
815 port3C = (port3C & 0x7F) | (value & 0x80);
817 }
else if ((port & 0xfc) == 0xfc) {
819 memMapRegs[port & 0x03] = value & 0x3f;
821 }
else if ((port & 0xff) == idControlPort()) {
824 }
else if (value ==
'S') {
826 }
else if (value ==
'H') {
827 configRegs[0x00] |= 1;
828 }
else if (value ==
'R') {
829 configRegs[0x00] &= ~1;
830 }
else if (
'0' <= value && value <=
'3') {
831 configRegs[0x00] &= ~(0b11 << 5);
832 configRegs[0x00] |=
byte((value -
'0') << 5);
833 }
else if (value ==
'A') {
834 shadowConfigRegs[0x1e] &= ~1;
835 }
else if (value ==
'M') {
836 shadowConfigRegs[0x1e] |= 1;
846 return memMapRegs[page];
852template<
typename Archive>
855 ar.template serializeBase<MSXDevice>(*
this);
856 ar.serialize(
"flash", flash,
859 "configRegs", configRegs,
860 "shadowConfigRegs", shadowConfigRegs,
861 "subSlotReg", subSlotReg,
867 if (ar.versionAtLeast(version, 3)) {
868 ar.serialize(
"psg", psg,
869 "psgLatch", psgLatch,
876 ar.serializePolymorphic(
"master", *ideDevices[0]);
877 ar.serializePolymorphic(
"slave", *ideDevices[1]);
878 ar.serialize(
"ideSoftReset", ideSoftReset,
879 "ideSelectedDevice", ideSelectedDevice,
880 "ideControlReg", ideControlReg,
882 "ideWrite", ideWrite,
884 "memMapRegs", memMapRegs,
887 "fmPacEnable", fmPacEnable,
888 "fmPacBank", fmPacBank,
889 "fmPac5ffe", fmPac5ffe,
890 "fmPac5fff", fmPac5fff);
892 if constexpr (Archive::IS_LOADER) {
894 writeSndLVL (configRegs[0x22], time);
895 writeCfgEEPR(configRegs[0x23], time);
897 auto backup24 = configRegs[0x24];
898 configRegs[0x24] = 0;
899 writePSGCtrl(backup24, time);
900 auto backup35 = configRegs[0x35];
901 configRegs[0x35] = 0xf0;
#define REGISTER_MSXDEVICE(CLASS, NAME)
void reset(EmuTime::param time)
void writeRegister(unsigned reg, uint8_t value, EmuTime::param time)
void write(size_t address, uint8_t value)
uint8_t read(size_t address)
uint8_t peek(size_t address) const
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
void reset(EmuTime::param time) override
This method is called on reset.
void writeMem(word address, byte value, EmuTime::param time) override
Write a given byte to a given location at a certain time to this device.
void serialize(Archive &ar, unsigned version)
byte readIO(word port, EmuTime::param time) override
Read a byte from an IO port at a certain time from this device.
void globalRead(word address, EmuTime::param time) override
Global reads.
void powerUp(EmuTime::param time) override
This method is called when MSX is powered up.
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Carnivore2(const DeviceConfig &config)
void writeIO(word port, byte value, EmuTime::param time) override
Write a byte to a given IO port at a certain time to this device.
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
byte getSelectedSegment(byte page) const override
void printWarning(std::string_view message)
static DummyAY8910Periphery & instance()
void write_CS(bool value, EmuTime::param time)
void write_CLK(bool value, EmuTime::param time)
bool read_DO(EmuTime::param time) const
void write_DI(bool value, EmuTime::param time)
void unregister_IO_In(byte port, MSXDevice *device)
void unregister_IO_Out(byte port, MSXDevice *device)
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
void invalidateDeviceRWCache()
Calls MSXCPUInterface::invalidateXXCache() for the specific (part of) the slot that this device is lo...
byte getPrimarySlot() const
EmuTime::param getCurrentTime() const
MSXCPUInterface & getCPUInterface() const
MSXCliComm & getCliComm() const
void setMode(Mode newMode)
void powerUp(EmuTime::param time)
uint8_t readMem(uint8_t address, EmuTime::param time)
void reset(EmuTime::param time)
void writeMem(uint8_t address, uint8_t value, EmuTime::param time)
void setSoftwareVolume(float volume, EmuTime::param time)
Change the 'software volume' of this sound device.
void writePort(bool port, byte value, EmuTime::param time)
void reset(EmuTime::param time)
std::unique_ptr< IDEDevice > create(const DeviceConfig &config)
This file implemented 3 utility functions:
uint8_t byte
8 bit unsigned integer
uint16_t word
16 bit unsigned integer
constexpr void iota(ForwardIt first, ForwardIt last, T value)
size_t size(std::string_view utf8)
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
constexpr auto xrange(T e)