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 return ay8910.readRegister(registerLatch, time);
63}
64
65byte MSXPSG::peekIO(word /*port*/, EmuTime::param time) const
66{
67 return ay8910.peekRegister(registerLatch, time);
68}
69
70void MSXPSG::writeIO(word port, byte value, EmuTime::param time)
71{
72 switch (port & 0x03) {
73 case 0:
74 registerLatch = value & addressMask;
75 break;
76 case 1:
77 ay8910.writeRegister(registerLatch, value, time);
78 break;
79 }
80}
81
82
83// AY8910Periphery
84byte MSXPSG::readA(EmuTime::param time)
85{
86 byte joystick = ports[selectedPort]->read(time) |
87 ((renShaTurbo.getSignal(time)) ? 0x10 : 0x00);
88
89#if 0
90 This breaks NinjaTap test programs.
91 Perhaps this behavior is different between MSX machines with a discrete
92 AY8910 chip versus machines with the PSG integrated in the MSX-engine?
93
94 // pin 6,7 input is ANDed with pin 6,7 output
95 byte pin67 = prev << (4 - 2 * selectedPort);
96 joystick &= (pin67| 0xCF);
97#endif
98
99 byte cassetteInput = cassette.cassetteIn(time) ? 0x80 : 0x00;
100 return joystick | keyLayout | cassetteInput;
101}
102
103void MSXPSG::writeB(byte value, EmuTime::param time)
104{
105 byte val0 = (value & 0x03) | ((value & 0x10) >> 2);
106 byte val1 = ((value & 0x0C) >> 2) | ((value & 0x20) >> 3);
107 ports[0]->write(val0, time);
108 ports[1]->write(val1, time);
109 selectedPort = (value & 0x40) >> 6;
110
111 if ((prev ^ value) & 0x80) {
112 getLedStatus().setLed(LedStatus::KANA, !(value & 0x80));
113 }
114 prev = value;
115}
116
117// version 1: initial version
118// version 2: joystickportA/B moved from here to MSXMotherBoard
119template<typename Archive>
120void MSXPSG::serialize(Archive& ar, unsigned version)
121{
122 ar.template serializeBase<MSXDevice>(*this);
123 ar.serialize("ay8910", ay8910);
124 if (ar.versionBelow(version, 2)) {
125 assert(Archive::IS_LOADER);
126 // in older versions there were always 2 real joystick ports
127 ar.serialize("joystickportA", *checked_cast<JoystickPort*>(ports[0]),
128 "joystickportB", *checked_cast<JoystickPort*>(ports[1]));
129 }
130 ar.serialize("registerLatch", registerLatch);
131 byte portB = prev;
132 ar.serialize("portB", portB);
133 if constexpr (Archive::IS_LOADER) {
134 writeB(portB, getCurrentTime());
135 }
136 // selectedPort is derived from portB
137}
140
141} // namespace openmsx
void test(const IterableBitSet< N > &s, std::initializer_list< size_t > list)
This class implements the AY-3-8910 sound chip.
Definition: AY8910.hh:21
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:41
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:70
void serialize(Archive &ar, unsigned version)
Definition: MSXPSG.cc:120
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:65
bool getSignal(EmuTime::param time)
Get the output signal in negative logic.
Definition: RenShaTurbo.cc:23
std::string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:730
This file implemented 3 utility functions:
Definition: Autofire.cc:9
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:1021