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_InOut(idControlPort(),
this);
60 writeSndLVL(0x1b, time);
61 writePSGCtrl(0x1b, time);
73 configRegs[0x00] = 0x30;
74 for (
int i : {0x01, 0x02, 0x03}) configRegs[i] = 0;
75 configRegs[0x05] = shadowConfigRegs[0x05] = 0;
77 configRegs[0x06] = shadowConfigRegs[0x06] = 0xF8;
78 configRegs[0x07] = shadowConfigRegs[0x07] = 0x50;
79 configRegs[0x08] = shadowConfigRegs[0x08] = 0x00;
80 configRegs[0x09] = shadowConfigRegs[0x09] = 0x85;
81 configRegs[0x0a] = shadowConfigRegs[0x0a] = 0x03;
82 configRegs[0x0b] = shadowConfigRegs[0x0b] = 0x40;
84 for (
int i : {0x0f, 0x15, 0x1b}) {
85 configRegs[i] = shadowConfigRegs[i] = 0;
88 configRegs[0x1e] = shadowConfigRegs[0x1e] = 0xff;
89 configRegs[0x20] = 0x02;
90 configRegs[0x28] = 0b11'10'01'00;
94 writeCfgEEPR(0, time);
107 ideSelectedDevice = 0;
108 ideSoftReset =
false;
109 ideDevices[0]->reset(time);
110 ideDevices[1]->reset(time);
128 if (!delayedConfig())
return;
130 if ((!delayedConfig4000() && (address == 0x0000) && (
getCPU().isM1Cycle(address))) ||
131 ( delayedConfig4000() && (address <= 0x4000) && (address < 0x4010))) {
133 for (
auto i :
xrange(0x05, 0x1f)) {
134 configRegs[i] = shadowConfigRegs[i];
139Carnivore2::SubDevice Carnivore2::getSubDevice(
word address)
const
143 if (slotExpanded()) {
144 auto page = narrow<byte>(address >> 14);
145 byte selectedSubSlot = (subSlotReg >> (2 * page)) & 0x03;
146 if (subSlotEnabled(selectedSubSlot)) {
147 subSlot = selectedSubSlot;
151 if (subSlotEnabled(i)) {
158 using enum SubDevice;
159 if (subSlot == (configRegs[0x28] & 0b00'00'00'11) >> 0) {
161 }
else if (subSlot == (configRegs[0x28] & 0b00'00'11'00) >> 2) {
163 }
else if (subSlot == (configRegs[0x28] & 0b00'11'00'00) >> 4) {
165 }
else if (subSlot == (configRegs[0x28] & 0b11'00'00'00) >> 6) {
174 if (slotExpanded() && (address == 0xffff)) {
175 return subSlotReg ^ 0xff;
177 switch (getSubDevice(address)) {
178 using enum SubDevice;
179 case MultiMapper:
return readMultiMapperSlot(address, time);
180 case IDE:
return readIDESlot(address, time);
181 case MemoryMapper:
return readMemoryMapperSlot(address);
182 case FmPac:
return readFmPacSlot(address);
183 default:
return 0xff;
189 if (slotExpanded() && (address == 0xffff)) {
190 return subSlotReg ^ 0xff;
192 switch (getSubDevice(address)) {
193 using enum SubDevice;
194 case MultiMapper:
return peekMultiMapperSlot(address, time);
195 case IDE:
return peekIDESlot(address, time);
196 case MemoryMapper:
return peekMemoryMapperSlot(address);
197 case FmPac:
return peekFmPacSlot(address);
198 default:
return 0xff;
204 if (slotExpanded() && (address == 0xffff)) {
209 switch (getSubDevice(address)) {
210 using enum SubDevice;
212 writeMultiMapperSlot(address, value, time);
215 writeIDESlot(address, value, time);
218 writeMemoryMapperSlot(address, value);
221 writeFmPacSlot(address, value, time);
229unsigned Carnivore2::getDirectFlashAddr()
const
231 return (configRegs[0x01] << 0) |
232 (configRegs[0x02] << 8) |
233 (configRegs[0x03] << 16);
236byte Carnivore2::peekConfigRegister(
word address, EmuTime::param time)
const
239 if ((0x05 <= address) && (address <= 0x1e)) {
242 return shadowConfigRegs[address];
245 case 0x04:
return flash.
peek(getDirectFlashAddr());
246 case 0x1f:
return configRegs[0x00];
247 case 0x23:
return byte(configRegs[address] |
249 case 0x2C:
return '2';
250 case 0x2D:
return '5';
251 case 0x2E:
return '0';
252 default:
return configRegs[address];
257byte Carnivore2::readConfigRegister(
word address, EmuTime::param time)
260 if (address == 0x04) {
261 return flash.
read(getDirectFlashAddr());
263 return peekConfigRegister(address, time);
267static constexpr float volumeLevel(
byte volume)
269 constexpr std::array<byte, 8> tab = {5, 6, 7, 8, 10, 12, 14, 16};
270 return narrow<float>(tab[volume & 7]) * (1.0f / 16.0f);
273void Carnivore2::writeSndLVL(
byte value, EmuTime::param time)
275 configRegs[0x22] = value;
280void Carnivore2::writeCfgEEPR(
byte value, EmuTime::param time)
282 configRegs[0x23] = value & 0x0e;
288void Carnivore2::writePSGCtrl(
byte value, EmuTime::param time)
291 if ((value ^ configRegs[0x24]) & 0x80) {
292 byte ioBase = (configRegs[0x30] & 0x01) ? 0x10 : 0xa0;
299 configRegs[0x24] = value;
303void Carnivore2::writePSGAlt(
byte value)
305 if ((value ^ configRegs[0x30]) & 0x01) {
306 if (configRegs[0x24] & 0x80) {
307 byte ioBaseOld = (configRegs[0x30] & 0x01) ? 0x10 : 0xa0;
308 byte ioBaseNew = (value & 0x01) ? 0x10 : 0xa0;
313 configRegs[0x30] = value;
316void Carnivore2::writePFXN(
byte value)
318 byte oldPort = idControlPort();
319 configRegs[0x35] = 0xf0 | (value & 0b11);
320 if (
auto newPort = idControlPort(); newPort != oldPort) {
327[[nodiscard]]
static bool bitPairsUnique(uint8_t x)
330 for (
int i = 0; i < 4; ++i) {
331 seen |= 1 << (x & 3);
334 return seen == 0b1111;
337void Carnivore2::writeConfigRegister(
word address,
byte value, EmuTime::param time)
340 if ((0x05 <= address) && (address <= 0x1e)) {
342 if (address == 0x05) value &= 0x7f;
343 if ((address == 0x1e) && ((value & 0x8f) == 0x0f))
return;
345 shadowConfigRegs[address] = value;
346 if (!delayedConfig()) configRegs[address] = value;
349 case 0x03: configRegs[address] = value & 0x7f;
break;
350 case 0x04: flash.
write(getDirectFlashAddr(), value);
break;
351 case 0x1f: configRegs[0x00] = value;
break;
352 case 0x20: configRegs[address] = value & 0x07;
break;
353 case 0x22: writeSndLVL(value, time);
break;
354 case 0x23: writeCfgEEPR(value, time);
break;
355 case 0x24: writePSGCtrl(value, time);
break;
356 case 0x30: writePSGAlt(value);
break;
357 case 0x35: writePFXN(value);
break;
359 if (!bitPairsUnique(value)) {
361 "Illegal value of ", value,
362 "written to SLM_cfg register");
365 default: configRegs[address] = value;
break;
370bool Carnivore2::isConfigReg(
word address)
const
372 if (configRegs[0x00] & 0x80)
return false;
373 unsigned base = ((configRegs[0x00] & 0x60) << 9) | 0xF80;
374 return (base <= address) && (address < (base + 0x40));
377std::pair<unsigned, byte> Carnivore2::decodeMultiMapper(
word address)
const
380 for (
auto i :
xrange(4)) {
381 auto base = subspan<6>(configRegs, (i * 6) + 6);
383 if (mult & 8)
continue;
385 byte sizeCode = mult & 7;
386 if (sizeCode < 3)
continue;
389 bool mirroringDisabled = mult & 0x40;
390 static constexpr std::array checkMasks = {
391 std::array<byte, 8>{0x00, 0x00, 0x00, 0x30, 0x60, 0xc0, 0x80, 0x00},
392 std::array<byte, 8>{0x00, 0x00, 0x00, 0xf0, 0xe0, 0xc0, 0x80, 0x00},
394 byte checkMask = checkMasks[mirroringDisabled][sizeCode];
395 if (((address >> 8) & checkMask) != (base[5] & checkMask))
continue;
398 byte bank = base[2] & base[4];
399 unsigned size = 512 << sizeCode;
400 unsigned addr = (bank *
size) | (address & (size - 1));
401 addr += configRegs[0x05] * 0x10000;
405 return {unsigned(-1),
byte(-1)};
408bool Carnivore2::sccAccess(
word address)
const
410 if (!sccEnabled())
return false;
411 if (sccMode & 0x20) {
413 return (0xb800 <= address) && (address < 0xc000) &&
414 ((sccBank[3] & 0x80) == 0x80);
417 return (0x9800 <= address) && (address < 0xa000) &&
418 ((sccBank[2] & 0x3f) == 0x3f);
422byte Carnivore2::readMultiMapperSlot(
word address, EmuTime::param time)
424 if (isConfigReg(address)) {
425 return readConfigRegister(address, time);
427 if (sccAccess(address)) {
428 return scc.
readMem(narrow_cast<uint8_t>(address & 0xff), time);
431 auto [addr, mult] = decodeMultiMapper(address);
432 if (addr ==
unsigned(-1))
return 0xff;
435 return ram[addr & 0x1fffff];
437 return flash.
read(addr);
441byte Carnivore2::peekMultiMapperSlot(
word address, EmuTime::param time)
const
443 if (isConfigReg(address)) {
444 return peekConfigRegister(address, time);
447 auto [addr, mult] = decodeMultiMapper(address);
448 if (addr ==
unsigned(-1))
return 0xff;
451 return ram[addr & 0x1fffff];
453 return flash.
peek(addr);
457void Carnivore2::writeMultiMapperSlot(
word address,
byte value, EmuTime::param time)
459 if (isConfigReg(address)) {
461 return writeConfigRegister(address, value, time);
465 for (
auto i :
xrange(4)) {
466 auto base = subspan<6>(configRegs, (i * 6) + 6);
471 if (((address >> 8) & mask) == (addr & mask)) {
473 configRegs[(i * 6) + 6 + 2] = value;
474 shadowConfigRegs[(i * 6) + 6 + 2] = value;
479 auto [addr, mult] = decodeMultiMapper(address);
480 if ((addr !=
unsigned(-1)) && (mult & 0x10)) {
482 ram[addr & 0x1fffff] = value;
484 flash.
write(addr, value);
488 if (sccEnabled() && ((address | 1) == 0xbfff)) {
493 if (((sccMode & 0x10) == 0x00) &&
494 ((address & 0x1800) == 0x1000)) {
495 auto region = narrow<byte>((address >> 13) - 2);
496 sccBank[region] = value;
497 }
else if (sccAccess(address)) {
498 scc.
writeMem(narrow_cast<uint8_t>(address & 0xff), value, time);
502byte Carnivore2::readIDESlot(
word address, EmuTime::param time)
505 if (ideRegsEnabled() && ((address & 0xfe00) == 0x7c00)) {
507 switch (address & 1) {
509 auto tmp = ideReadData(time);
510 ideRead = narrow_cast<byte>(tmp >> 8);
511 return narrow_cast<byte>(tmp & 0xff);
517 if (ideRegsEnabled() && ((address & 0xff00) == 0x7e00)) {
519 return ideReadReg(address & 0xf, time);
521 if ((0x4000 <= address) && (address < 0x8000)) {
523 unsigned addr = (address & 0x3fff) + (ideBank() * 0x4000) + 0x10000;
524 if (readBIOSfromRAM()) {
527 return flash.
read(addr);
533byte Carnivore2::peekIDESlot(
word address, EmuTime::param )
const
535 if (ideRegsEnabled() && ((address & 0xfe00) == 0x7c00)) {
539 if (ideRegsEnabled() && ((address & 0xff00) == 0x7e00)) {
543 if ((0x4000 <= address) && (address < 0x8000)) {
545 unsigned addr = (address & 0x3fff) + (ideBank() * 0x4000) + 0x10000;
546 if (readBIOSfromRAM()) {
549 return flash.
peek(addr);
555void Carnivore2::writeIDESlot(
word address,
byte value, EmuTime::param time)
558 if (address == 0x4104) {
559 ideControlReg = value;
561 }
else if (ideRegsEnabled() && ((address & 0xfe00) == 0x7c00)) {
563 switch (address & 1) {
568 auto tmp =
word((value << 8) | ideWrite);
569 ideWriteData(tmp, time);
574 }
else if (ideRegsEnabled() && ((address & 0xff00) == 0x7e00)) {
576 ideWriteReg(address & 0xf, value, time);
580word Carnivore2::ideReadData(EmuTime::param time)
582 return ideDevices[ideSelectedDevice]->readData(time);
585void Carnivore2::ideWriteData(
word value, EmuTime::param time)
587 ideDevices[ideSelectedDevice]->writeData(value, time);
590byte Carnivore2::ideReadReg(
byte reg, EmuTime::param time)
592 if (reg == 14) reg = 7;
602 return narrow_cast<byte>(ideReadData(time) & 0xff);
604 auto result = ideDevices[ideSelectedDevice]->readReg(reg, time);
606 result = (result & 0xef) | (ideSelectedDevice ? 0x10 : 0x00);
613void Carnivore2::ideWriteReg(
byte reg,
byte value, EmuTime::param time)
616 if ((reg == 14) && !(value & 0x04)) {
618 ideSoftReset =
false;
623 ideWriteData(narrow_cast<word>((value << 8) | value), time);
625 if ((reg == 14) && (value & 0x04)) {
628 ideDevices[0]->reset(time);
629 ideDevices[1]->reset(time);
632 ideSelectedDevice = (value & 0x10) ? 1 : 0;
634 ideDevices[ideSelectedDevice]->writeReg(reg, value, time);
640bool Carnivore2::isMemMapControl(
word address)
const
642 return (port3C & 0x80) &&
643 (( (port3C & 0x08) && ((address & 0xc000) == 0x4000)) ||
644 (!(port3C & 0x08) && ((address & 0xc000) == 0x8000)));
647unsigned Carnivore2::getMemoryMapperAddress(
word address)
const
649 return (address & 0x3fff) +
650 0x4000 * memMapRegs[address >> 14] +
654bool Carnivore2::isMemoryMapperWriteProtected(
word address)
const
656 auto page = address >> 14;
657 return (port3C & (1 << page)) != 0;
660byte Carnivore2::peekMemoryMapperSlot(
word address)
const
662 if (isMemMapControl(address)) {
663 switch (address & 0xff) {
666 case 0xfc:
case 0xfd:
case 0xfe:
case 0xff:
667 return memMapRegs[address & 0x03];
670 return ram[getMemoryMapperAddress(address)];
673byte Carnivore2::readMemoryMapperSlot(
word address)
const
675 return peekMemoryMapperSlot(address);
678void Carnivore2::writeMemoryMapperSlot(
word address,
byte value)
680 if (isMemMapControl(address)) {
681 switch (address & 0xff) {
683 value |= (value & 0x02) << 6;
686 case 0xfc:
case 0xfd:
case 0xfe:
case 0xff:
687 memMapRegs[address & 0x03] = value & 0x3f;
691 if (!isMemoryMapperWriteProtected(address)) {
692 ram[getMemoryMapperAddress(address)] = value;
696byte Carnivore2::readFmPacSlot(
word address)
698 if (address == 0x7ff6) {
700 }
else if (address == 0x7ff7) {
702 }
else if ((0x4000 <= address) && (address < 0x8000)) {
703 if (fmPacSramEnabled()) {
704 if (address < 0x5ffe) {
705 return ram[(address & 0x1fff) | 0xfe000];
706 }
else if (address == 0x5ffe) {
708 }
else if (address == 0x5fff) {
714 unsigned addr = (address & 0x3fff) + (0x4000 * fmPacBank) + 0x30000;
715 if (readBIOSfromRAM()) {
718 return flash.
read(addr);
725byte Carnivore2::peekFmPacSlot(
word address)
const
727 if (address == 0x7ff6) {
729 }
else if (address == 0x7ff7) {
731 }
else if ((0x4000 <= address) && (address < 0x8000)) {
732 if (fmPacSramEnabled()) {
733 if (address < 0x5ffe) {
734 return ram[(address & 0x1fff) | 0xfe000];
735 }
else if (address == 0x5ffe) {
737 }
else if (address == 0x5fff) {
743 unsigned addr = (address & 0x3fff) + (0x4000 * fmPacBank) + 0x30000;
744 if (readBIOSfromRAM()) {
747 return flash.
peek(addr);
754void Carnivore2::writeFmPacSlot(
word address,
byte value, EmuTime::param time)
756 if ((0x4000 <= address) && (address < 0x5ffe)) {
757 if (fmPacSramEnabled()) {
758 ram[(address & 0x1fff) | 0xfe000] = value;
760 }
else if (address == 0x5ffe) {
762 }
else if (address == 0x5fff) {
764 }
else if (address ==
one_of(0x7ff4, 0x7ff5)) {
765 ym2413.
writePort(address & 1, value, time);
766 }
else if (address == 0x7ff6) {
767 fmPacEnable = value & 0x11;
768 }
else if (address == 0x7ff7) {
769 fmPacBank = value & 0x03;
775 return peekIO(port, time);
781 if (memMapReadEnabled() && ((port & 0xfc) == 0xfc)) {
784 }
else if ((port & 0xff) == idControlPort() && PF0_RV != 0) {
787 }
else if (PF0_RV == 2) {
797 if (((port & 0xff) == 0xa0) || ((port & 0xff) == 0x10)) {
798 psgLatch = value & 0x0f;
799 }
else if ((port & 0xff) == 0xa1 || (port & 0xff) == 0x11) {
801 }
else if (((port & 0xfe) == 0x7c) &&
802 (fmPacPortEnabled1() || fmPacPortEnabled2())) {
805 }
else if (((port & 0xff) == 0x3c) && writePort3cEnabled()) {
807 port3C = (port3C & 0x7F) | (value & 0x80);
809 }
else if ((port & 0xfc) == 0xfc) {
811 memMapRegs[port & 0x03] = value & 0x3f;
813 }
else if ((port & 0xff) == idControlPort()) {
816 }
else if (value ==
'S') {
818 }
else if (value ==
'H') {
819 configRegs[0x00] |= 1;
820 }
else if (value ==
'R') {
821 configRegs[0x00] &= ~1;
822 }
else if (
'0' <= value && value <=
'3') {
823 configRegs[0x00] &= ~(0b11 << 5);
824 configRegs[0x00] |=
byte((value -
'0') << 5);
825 }
else if (value ==
'A') {
826 shadowConfigRegs[0x1e] &= ~1;
827 }
else if (value ==
'M') {
828 shadowConfigRegs[0x1e] |= 1;
838 return memMapRegs[page];
844template<
typename Archive>
847 ar.template serializeBase<MSXDevice>(*
this);
848 ar.serialize(
"flash", flash,
851 "configRegs", configRegs,
852 "shadowConfigRegs", shadowConfigRegs,
853 "subSlotReg", subSlotReg,
859 if (ar.versionAtLeast(version, 3)) {
860 ar.serialize(
"psg", psg,
861 "psgLatch", psgLatch,
868 ar.serializePolymorphic(
"master", *ideDevices[0]);
869 ar.serializePolymorphic(
"slave", *ideDevices[1]);
870 ar.serialize(
"ideSoftReset", ideSoftReset,
871 "ideSelectedDevice", ideSelectedDevice,
872 "ideControlReg", ideControlReg,
874 "ideWrite", ideWrite,
876 "memMapRegs", memMapRegs,
879 "fmPacEnable", fmPacEnable,
880 "fmPacBank", fmPacBank,
881 "fmPac5ffe", fmPac5ffe,
882 "fmPac5fff", fmPac5fff);
884 if constexpr (Archive::IS_LOADER) {
886 writeSndLVL (configRegs[0x22], time);
887 writeCfgEEPR(configRegs[0x23], time);
889 auto backup24 = configRegs[0x24];
890 configRegs[0x24] = 0;
891 writePSGCtrl(backup24, time);
892 auto backup35 = configRegs[0x35];
893 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_Out_range(byte port, unsigned num, MSXDevice *device)
void unregister_IO_InOut(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)