openMSX
MSXPSG.cc
Go to the documentation of this file.
1#include "MSXPSG.hh"
2#include "LedStatus.hh"
3#include "CassettePort.hh"
4#include "MSXMotherBoard.hh"
5#include "JoystickPort.hh"
6#include "RenShaTurbo.hh"
7#include "StringOp.hh"
8#include "serialize.hh"
9#include "checked_cast.hh"
10#include "stl.hh"
11
12namespace openmsx {
13
14[[nodiscard]] static byte getKeyboardLayout(const MSXPSG& psg)
15{
16 // As many (mostly European) configs do not specify the layout
17 // in the PSG config, assume 50on for these cases.
18 auto value = psg.getDeviceConfig().getChildData("keyboardlayout", "50on");
19 StringOp::casecmp cmp; // case-insensitive
20 if (cmp(value, "50on")) {
21 return 0x00;
22 } else if (cmp(value, "jis")) {
23 return 0x40;
24 }
25 throw MSXException(
26 "Illegal keyboard layout configuration in '", psg.getName(),
27 "' device configuration: '", value,
28 "', expected 'jis' or '50on'.");
29}
30
31// MSXDevice
33 : MSXDevice(config)
34 , cassette(getMotherBoard().getCassettePort())
35 , renShaTurbo(getMotherBoard().getRenShaTurbo())
36 , ports(generate_array<2>([&](auto i) { return &getMotherBoard().getJoystickPort(unsigned(i)); }))
37 , keyLayout(getKeyboardLayout(*this))
38 , addressMask(config.getChildDataAsBool("mirrored_registers", true) ? 0x0f : 0xff)
39 , ay8910(getName(), *this, config, getCurrentTime())
40{
41 reset(getCurrentTime());
42}
43
45{
46 powerDown(EmuTime::dummy());
47}
48
49void MSXPSG::reset(EmuTime::param time)
50{
51 registerLatch = 0;
52 ay8910.reset(time);
53}
54
55void MSXPSG::powerDown(EmuTime::param /*time*/)
56{
58}
59
60byte MSXPSG::readIO(word port, EmuTime::param time)
61{
62 switch (port & 0x03) {
63 case 2:
64 return ay8910.readRegister(registerLatch, time);
65 default:
66 // nothing for 0, 1 and 3
67 return 0xFF;
68 }
69}
70
71byte MSXPSG::peekIO(word /*port*/, EmuTime::param time) const
72{
73 return ay8910.peekRegister(registerLatch, time);
74}
75
76void MSXPSG::writeIO(word port, byte value, EmuTime::param time)
77{
78 switch (port & 0x03) {
79 case 0:
80 registerLatch = value & addressMask;
81 break;
82 case 1:
83 ay8910.writeRegister(registerLatch, value, time);
84 break;
85 }
86}
87
88
89// AY8910Periphery
90byte MSXPSG::readA(EmuTime::param time)
91{
92 byte joystick = ports[selectedPort]->read(time) |
93 ((renShaTurbo.getSignal(time)) ? 0x10 : 0x00);
94
95#if 0
96 This breaks NinjaTap test programs.
97 Perhaps this behavior is different between MSX machines with a discrete
98 AY8910 chip versus machines with the PSG integrated in the MSX-engine?
99
100 // pin 6,7 input is ANDed with pin 6,7 output
101 byte pin67 = prev << (4 - 2 * selectedPort);
102 joystick &= (pin67| 0xCF);
103#endif
104
105 byte cassetteInput = cassette.cassetteIn(time) ? 0x80 : 0x00;
106 return joystick | keyLayout | cassetteInput;
107}
108
109void MSXPSG::writeB(byte value, EmuTime::param time)
110{
111 byte val0 = (value & 0x03) | ((value & 0x10) >> 2);
112 byte val1 = ((value & 0x0C) >> 2) | ((value & 0x20) >> 3);
113 ports[0]->write(val0, time);
114 ports[1]->write(val1, time);
115 selectedPort = (value & 0x40) >> 6;
116
117 if ((prev ^ value) & 0x80) {
118 getLedStatus().setLed(LedStatus::KANA, !(value & 0x80));
119 }
120 prev = value;
121}
122
123// version 1: initial version
124// version 2: joystickportA/B moved from here to MSXMotherBoard
125template<typename Archive>
126void MSXPSG::serialize(Archive& ar, unsigned version)
127{
128 ar.template serializeBase<MSXDevice>(*this);
129 ar.serialize("ay8910", ay8910);
130 if (ar.versionBelow(version, 2)) {
131 assert(Archive::IS_LOADER);
132 // in older versions there were always 2 real joystick ports
133 ar.serialize("joystickportA", *checked_cast<JoystickPort*>(ports[0]),
134 "joystickportB", *checked_cast<JoystickPort*>(ports[1]));
135 }
136 ar.serialize("registerLatch", registerLatch);
137 byte portB = prev;
138 ar.serialize("portB", portB);
139 if constexpr (Archive::IS_LOADER) {
140 writeB(portB, getCurrentTime());
141 }
142 // selectedPort is derived from portB
143}
146
147} // namespace openmsx
void test(const IterableBitSet< N > &s, std::initializer_list< size_t > list)
#define REGISTER_MSXDEVICE(CLASS, NAME)
Definition MSXDevice.hh:356
This class implements the AY-3-8910 sound chip.
Definition AY8910.hh:22
uint8_t readRegister(unsigned reg, EmuTime::param time)
Definition AY8910.cc:523
void reset(EmuTime::param time)
Definition AY8910.cc:510
void writeRegister(unsigned reg, uint8_t value, EmuTime::param time)
Definition AY8910.cc:567
uint8_t peekRegister(unsigned reg, EmuTime::param time) const
Definition AY8910.cc:548
virtual bool cassetteIn(EmuTime::param time)=0
Reads one bit from the cassette port.
void setLed(Led led, bool status)
Definition LedStatus.cc:44
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition MSXDevice.hh:36
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition MSXDevice.cc:70
LedStatus & getLedStatus() const
Definition MSXDevice.cc:153
EmuTime::param getCurrentTime() const
Definition MSXDevice.cc:125
JoystickPortIf & getJoystickPort(unsigned port)
MSXPSG(const DeviceConfig &config)
Definition MSXPSG.cc:32
void powerDown(EmuTime::param time) override
This method is called when MSX is powered down.
Definition MSXPSG.cc:55
~MSXPSG() override
Definition MSXPSG.cc:44
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.
Definition MSXPSG.cc:76
void serialize(Archive &ar, unsigned version)
Definition MSXPSG.cc:126
void reset(EmuTime::param time) override
This method is called on reset.
Definition MSXPSG.cc:49
byte readIO(word port, EmuTime::param time) override
Read a byte from an IO port at a certain time from this device.
Definition MSXPSG.cc:60
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
Definition MSXPSG.cc:71
bool getSignal(EmuTime::param time)
Get the output signal in negative logic.
This file implemented 3 utility functions:
Definition Autofire.cc:11
uint16_t word
16 bit unsigned integer
Definition openmsx.hh:29
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)