13 static constexpr
auto sectorInfo = [] {
15 using Info = AmdFlash::SectorInfo;
16 std::array<Info, 8 + 127> result = {};
17 cstd::fill(result.begin(), result.begin() + 8,
Info{ 8 * 1024, false});
18 cstd::fill(result.begin() + 8, result.end(),
Info{64 * 1024, false});
25 , flash(
"Carnivore2 flash", sectorInfo, 0x207e,
26 AmdFlash::Addressing::BITS_12, config)
27 , ram(config,
getName() +
" ram",
"ram", 2048 * 1024)
30 , scc(
getName() +
" scc", config, getCurrentTime(),
SCC::SCC_Compatible)
31 , ym2413(
getName() +
" ym2413", config)
43 writeSndLVL(0x1b, time);
54 configRegs[0x00] = 0x30;
55 for (
int i : {0x01, 0x02, 0x03}) configRegs[i] = 0;
56 configRegs[0x05] = shadowConfigRegs[0x05] = 0;
58 configRegs[0x06] = shadowConfigRegs[0x06] = 0xF8;
59 configRegs[0x07] = shadowConfigRegs[0x07] = 0x50;
60 configRegs[0x08] = shadowConfigRegs[0x08] = 0x00;
61 configRegs[0x09] = shadowConfigRegs[0x09] = 0x85;
62 configRegs[0x0a] = shadowConfigRegs[0x0a] = 0x03;
63 configRegs[0x0b] = shadowConfigRegs[0x0b] = 0x40;
65 for (
int i : {0x0f, 0x15, 0x1b}) {
66 configRegs[i] = shadowConfigRegs[i] = 0;
69 configRegs[0x1e] = shadowConfigRegs[0x1e] = 0xff;
70 configRegs[0x20] = 0x02;
72 writeCfgEEPR(0, time);
81 ideSelectedDevice = 0;
83 ideDevices[0]->reset(time);
84 ideDevices[1]->reset(time);
99 if (!delayedConfig())
return;
101 if ((!delayedConfig4000() && (address == 0x0000) && (
getCPU().isM1Cycle(address))) ||
102 ( delayedConfig4000() && (address <= 0x4000) && (address < 0x4010))) {
104 for (
auto i :
xrange(0x05, 0x1f)) {
105 configRegs[i] = shadowConfigRegs[i];
110 byte Carnivore2::getSubSlot(
word address)
const
112 if (slotExpanded()) {
113 byte page = address >> 14;
114 byte subSlot = (subSlotReg >> (2 * page)) & 0x03;
115 return subSlotEnabled(subSlot) ? subSlot : -1;
117 for (
auto i :
xrange(4)) {
118 if (subSlotEnabled(i))
return i;
126 if (slotExpanded() && (address == 0xffff)) {
127 return subSlotReg ^ 0xff;
129 switch (getSubSlot(address)) {
130 case 0:
return readMultiMapperSlot(address, time);
131 case 1:
return readIDESlot(address, time);
132 case 2:
return readMemoryMapperSlot(address);
133 case 3:
return readFmPacSlot(address);
134 default:
return 0xff;
140 if (slotExpanded() && (address == 0xffff)) {
141 return subSlotReg ^ 0xff;
143 switch (getSubSlot(address)) {
144 case 0:
return peekMultiMapperSlot(address, time);
145 case 1:
return peekIDESlot(address, time);
146 case 2:
return peekMemoryMapperSlot(address);
147 case 3:
return peekFmPacSlot(address);
148 default:
return 0xff;
154 if (slotExpanded() && (address == 0xffff)) {
159 switch (getSubSlot(address)) {
160 case 0: writeMultiMapperSlot(address, value, time);
break;
161 case 1: writeIDESlot(address, value, time);
break;
162 case 2: writeMemoryMapperSlot(address, value);
break;
163 case 3: writeFmPacSlot(address, value, time);
break;
168 unsigned Carnivore2::getDirectFlashAddr()
const
170 return (configRegs[0x01] << 0) |
171 (configRegs[0x02] << 8) |
172 (configRegs[0x03] << 16);
175 byte Carnivore2::peekConfigRegister(
word address, EmuTime::param time)
const
178 if ((0x05 <= address) && (address <= 0x1e)) {
181 return shadowConfigRegs[address];
184 case 0x04:
return flash.
peek(getDirectFlashAddr());
185 case 0x1f:
return configRegs[0x00];
186 case 0x23:
return configRegs[address] |
188 default:
return configRegs[address];
193 byte Carnivore2::readConfigRegister(
word address, EmuTime::param time)
196 if (address == 0x04) {
197 return flash.
read(getDirectFlashAddr());
199 return peekConfigRegister(address, time);
203 static constexpr
float volumeLevel(
byte volume)
205 constexpr
byte tab[8] = {5, 6, 7, 8, 10, 12, 14, 16};
206 return tab[volume & 7] / 16.0f;
209 void Carnivore2::writeSndLVL(
byte value, EmuTime::param time)
211 configRegs[0x22] = value;
216 void Carnivore2::writeCfgEEPR(
byte value, EmuTime::param time)
218 configRegs[0x23] = value & 0x0e;
224 void Carnivore2::writeConfigRegister(
word address,
byte value, EmuTime::param time)
227 if ((0x05 <= address) && (address <= 0x1e)) {
229 if (address == 0x05) value &= 0x7f;
230 if ((address == 0x1e) && ((value & 0x8f) == 0x0f))
return;
232 shadowConfigRegs[address] = value;
233 if (!delayedConfig()) configRegs[address] = value;
236 case 0x03: configRegs[address] = value & 0x7f;
break;
237 case 0x04: flash.
write(getDirectFlashAddr(), value);
break;
238 case 0x1f: configRegs[0x00] = value;
break;
239 case 0x20: configRegs[address] = value & 0x07;
break;
240 case 0x22: writeSndLVL(value, time);
break;
241 case 0x23: writeCfgEEPR(value, time);
break;
243 default: configRegs[address] = value;
break;
248 bool Carnivore2::isConfigReg(
word address)
const
250 if (configRegs[0x00] & 0x80)
return false;
251 unsigned base = ((configRegs[0x00] & 0x60) << 9) | 0xF80;
252 return (base <= address) && (address < (base + 0x40));
255 std::pair<unsigned, byte> Carnivore2::decodeMultiMapper(
word address)
const
258 for (
auto i :
xrange(4)) {
259 const byte* base = configRegs + (i * 6) + 6;
261 if (mult & 8)
continue;
263 byte sizeCode = mult & 7;
264 if (sizeCode < 3)
continue;
267 bool mirroringDisabled = mult & 0x40;
268 static constexpr
byte checkMasks[2][8] = {
269 { 0x00, 0x00, 0x00, 0x30, 0x60, 0xc0, 0x80, 0x00 },
270 { 0x00, 0x00, 0x00, 0xf0, 0xe0, 0xc0, 0x80, 0x00 },
272 byte checkMask = checkMasks[mirroringDisabled][sizeCode];
273 if (((address >> 8) & checkMask) != (base[5] & checkMask))
continue;
276 byte bank = base[2] & base[4];
277 unsigned size = 512 << sizeCode;
278 unsigned addr = (bank *
size) | (address & (
size - 1));
279 addr += configRegs[0x05] * 0x10000;
283 return {unsigned(-1),
byte(-1)};
286 bool Carnivore2::sccAccess(
word address)
const
288 if (!sccEnabled())
return false;
289 if (sccMode & 0x20) {
291 return (0xb800 <= address) && (address < 0xc000) &&
292 ((sccBank[3] & 0x80) == 0x80);
295 return (0x9800 <= address) && (address < 0xa000) &&
296 ((sccBank[2] & 0x3f) == 0x3f);
300 byte Carnivore2::readMultiMapperSlot(
word address, EmuTime::param time)
302 if (isConfigReg(address)) {
303 return readConfigRegister(address, time);
306 auto [addr, mult] = decodeMultiMapper(address);
307 if (addr ==
unsigned(-1))
return 0xff;
310 return ram[addr & 0x1fffff];
312 return flash.
read(addr);
317 byte Carnivore2::peekMultiMapperSlot(
word address, EmuTime::param time)
const
319 if (isConfigReg(address)) {
320 return peekConfigRegister(address, time);
323 auto [addr, mult] = decodeMultiMapper(address);
324 if (addr ==
unsigned(-1))
return 0xff;
327 return ram[addr & 0x1fffff];
329 return flash.
peek(addr);
333 void Carnivore2::writeMultiMapperSlot(
word address,
byte value, EmuTime::param time)
335 if (isConfigReg(address)) {
337 return writeConfigRegister(address, value, time);
341 for (
auto i :
xrange(4)) {
342 byte* base = configRegs + (i * 6) + 6;
347 if (((address >> 8) &
mask) == (addr &
mask)) {
349 configRegs[(i * 6) + 6 + 2] = value;
350 shadowConfigRegs[(i * 6) + 6 + 2] = value;
355 auto [addr, mult] = decodeMultiMapper(address);
356 if ((addr !=
unsigned(-1)) && (mult & 0x10)) {
358 ram[addr & 0x1fffff] = value;
360 flash.
write(addr, value);
364 if (sccEnabled() && ((address | 1) == 0xbfff)) {
369 if (((sccMode & 0x10) == 0x00) &&
370 ((address & 0x1800) == 0x1000)) {
371 byte region = (address >> 13) - 2;
372 sccBank[region] = value;
373 }
else if (sccAccess(address)) {
374 scc.
writeMem(address & 0xff, value, time);
378 byte Carnivore2::readIDESlot(
word address, EmuTime::param time)
381 if (ideRegsEnabled() && ((address & 0xfe00) == 0x7c00)) {
383 switch (address & 1) {
385 auto tmp = ideReadData(time);
393 if (ideRegsEnabled() && ((address & 0xff00) == 0x7e00)) {
395 return ideReadReg(address & 0xf, time);
397 if ((0x4000 <= address) && (address < 0x8000)) {
399 unsigned addr = (address & 0x3fff) + (ideBank() * 0x4000) + 0x10000;
400 if (readBIOSfromRAM()) {
403 return flash.
read(addr);
409 byte Carnivore2::peekIDESlot(
word address, EmuTime::param )
const
411 if (ideRegsEnabled() && ((address & 0xfe00) == 0x7c00)) {
415 if (ideRegsEnabled() && ((address & 0xff00) == 0x7e00)) {
419 if ((0x4000 <= address) && (address < 0x8000)) {
421 unsigned addr = (address & 0x3fff) + (ideBank() * 0x4000) + 0x10000;
422 if (readBIOSfromRAM()) {
425 return flash.
peek(addr);
431 void Carnivore2::writeIDESlot(
word address,
byte value, EmuTime::param time)
434 if (address == 0x4104) {
435 ideControlReg = value;
437 }
else if (ideRegsEnabled() && ((address & 0xfe00) == 0x7c00)) {
439 switch (address & 1) {
444 word tmp = (value << 8) | ideWrite;
445 ideWriteData(tmp, time);
450 }
else if (ideRegsEnabled() && ((address & 0xff00) == 0x7e00)) {
452 ideWriteReg(address & 0xf, value, time);
456 word Carnivore2::ideReadData(EmuTime::param time)
458 return ideDevices[ideSelectedDevice]->readData(time);
461 void Carnivore2::ideWriteData(
word value, EmuTime::param time)
463 ideDevices[ideSelectedDevice]->writeData(value, time);
466 byte Carnivore2::ideReadReg(
byte reg, EmuTime::param time)
468 if (reg == 14) reg = 7;
478 return ideReadData(time) & 0xff;
480 auto result = ideDevices[ideSelectedDevice]->readReg(reg, time);
482 result = (result & 0xef) | (ideSelectedDevice ? 0x10 : 0x00);
489 void Carnivore2::ideWriteReg(
byte reg,
byte value, EmuTime::param time)
492 if ((reg == 14) && !(value & 0x04)) {
494 ideSoftReset =
false;
499 ideWriteData((value << 8) | value, time);
501 if ((reg == 14) && (value & 0x04)) {
504 ideDevices[0]->reset(time);
505 ideDevices[1]->reset(time);
508 ideSelectedDevice = (value & 0x10) ? 1 : 0;
510 ideDevices[ideSelectedDevice]->writeReg(reg, value, time);
516 bool Carnivore2::isMemmapControl(
word address)
const
518 return (port3C & 0x80) &&
519 (( (port3C & 0x08) && ((address & 0xc000) == 0x4000)) ||
520 (!(port3C & 0x08) && ((address & 0xc000) == 0x8000)));
523 unsigned Carnivore2::getMemoryMapperAddress(
word address)
const
525 return (address & 0x3fff) +
526 0x4000 * memMapRegs[address >> 14] +
530 bool Carnivore2::isMemoryMapperWriteProtected(
word address)
const
532 byte page = address >> 14;
533 return (port3C & (1 << page)) != 0;
536 byte Carnivore2::peekMemoryMapperSlot(
word address)
const
538 if (isMemmapControl(address)) {
539 switch (address & 0xff) {
542 case 0xfc:
case 0xfd:
case 0xfe:
case 0xff:
543 return memMapRegs[address & 0x03];
546 return ram[getMemoryMapperAddress(address)];
549 byte Carnivore2::readMemoryMapperSlot(
word address)
551 return peekMemoryMapperSlot(address);
554 void Carnivore2::writeMemoryMapperSlot(
word address,
byte value)
556 if (isMemmapControl(address)) {
557 switch (address & 0xff) {
559 value |= (value & 0x02) << 6;
562 case 0xfc:
case 0xfd:
case 0xfe:
case 0xff:
563 memMapRegs[address & 0x03] = value & 0x3f;
567 if (!isMemoryMapperWriteProtected(address)) {
568 ram[getMemoryMapperAddress(address)] = value;
572 byte Carnivore2::readFmPacSlot(
word address)
574 if (address == 0x7ff6) {
576 }
else if (address == 0x7ff7) {
578 }
else if ((0x4000 <= address) && (address < 0x8000)) {
579 if (fmPacSramEnabled()) {
580 if (address < 0x5ffe) {
581 return ram[(address & 0x1fff) | 0xfe000];
582 }
else if (address == 0x5ffe) {
584 }
else if (address == 0x5fff) {
590 unsigned addr = (address & 0x3fff) + (0x4000 * fmPacBank) + 0x30000;
591 if (readBIOSfromRAM()) {
594 return flash.
read(addr);
601 byte Carnivore2::peekFmPacSlot(
word address)
const
603 if (address == 0x7ff6) {
605 }
else if (address == 0x7ff7) {
607 }
else if ((0x4000 <= address) && (address < 0x8000)) {
608 if (fmPacSramEnabled()) {
609 if (address < 0x5ffe) {
610 return ram[(address & 0x1fff) | 0xfe000];
611 }
else if (address == 0x5ffe) {
613 }
else if (address == 0x5fff) {
619 unsigned addr = (address & 0x3fff) + (0x4000 * fmPacBank) + 0x30000;
620 if (readBIOSfromRAM()) {
623 return flash.
peek(addr);
630 void Carnivore2::writeFmPacSlot(
word address,
byte value, EmuTime::param time)
632 if ((0x4000 <= address) && (address < 0x5ffe)) {
633 if (fmPacSramEnabled()) {
634 ram[(address & 0x1fff) | 0xfe000] = value;
636 }
else if (address == 0x5ffe) {
638 }
else if (address == 0x5fff) {
640 }
else if (address ==
one_of(0x7ff4, 0x7ff5)) {
641 ym2413.
writePort(address & 1, value, time);
642 }
else if (address == 0x7ff6) {
643 fmPacEnable = value & 0x11;
644 }
else if (address == 0x7ff7) {
645 fmPacBank = value & 0x03;
651 return peekIO(port, time);
657 if (memMapReadEnabled() && ((port & 0xfc) == 0xfc)) {
666 if (((port & 0xfe) == 0x7c) &&
667 (fmPacPortEnabled1() || fmPacPortEnabled2())) {
670 }
else if (((port & 0xff) == 0x3c) && writePort3cEnabled()) {
672 port3C = (port3C & 0x7F) | (value & 0x80);
674 }
else if ((port & 0xfc) == 0xfc) {
676 memMapRegs[port & 0x03] = value & 0x3f;
683 return memMapRegs[page];
688 template<
typename Archive>
691 ar.template serializeBase<MSXDevice>(*
this);
692 ar.serialize(
"flash", flash,
695 "configRegs", configRegs,
696 "shadowConfigRegs", shadowConfigRegs,
697 "subSlotReg", subSlotReg,
704 ar.serializePolymorphic(
"master", *ideDevices[0]);
705 ar.serializePolymorphic(
"slave", *ideDevices[1]);
706 ar.serialize(
"ideSoftReset", ideSoftReset,
707 "ideSelectedDevice", ideSelectedDevice,
708 "ideControlReg", ideControlReg,
710 "ideWrite", ideWrite,
712 "memMapRegs", memMapRegs,
715 "fmPacEnable", fmPacEnable,
716 "fmPacBank", fmPacBank,
717 "fmPac5ffe", fmPac5ffe,
718 "fmPac5fff", fmPac5fff);
722 writeSndLVL (configRegs[0x22], time);
723 writeCfgEEPR(configRegs[0x23], time);
void write(unsigned address, byte value)
byte peek(unsigned address) const
byte read(unsigned 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
const XMLElement * findChild(std::string_view name) const
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)
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...
EmuTime::param getCurrentTime() const
void writeMem(byte address, byte value, EmuTime::param time)
void setChipMode(ChipMode newMode)
void powerUp(EmuTime::param time)
void reset(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)
constexpr void fill(ForwardIt first, ForwardIt last, const T &value)
std::unique_ptr< IDEDevice > create(const DeviceConfig &config)
string getName(KeyCode keyCode)
Translate key code to key name.
This file implemented 3 utility functions:
uint8_t byte
8 bit unsigned integer
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
AmdFlash::SectorInfo Info
uint16_t word
16 bit unsigned integer
constexpr nibble mask[4][13]
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)