OXYGENHUB
◆ main openmsx / src / PasswordCart.cc
1.4 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
/*
2
 * Password Cartridge
3
 *
4
 * Access: write 0x00 to I/O port 0x7e
5
 *         provide 0xaa, <char1>, <char2>, continuous 0xff sequence reading I/O port 0x7e
6
 *         write any non-zero to I/O port 0x7e
7
 *         provide 0xff at all time reading I/O port 0x7e
8
 */
9
10
#include "PasswordCart.hh"
11
#include "narrow.hh"
12
#include "serialize.hh"
13
#include <algorithm>
14
15
namespace openmsx {
16
17
PasswordCart::PasswordCart(const DeviceConfig& config)
18
	: MSXDevice(config)
19
	, password(narrow_cast<uint16_t>(config.getChildDataAsInt("password", 0)))
20
{
21
	reset(EmuTime::dummy());
22
}
23
24
void PasswordCart::reset(EmuTime /*time*/)
25
{
26
	pointer = 3;
27
}
28
29
void PasswordCart::writeIO(uint16_t /*port*/, byte value, EmuTime /*time*/)
30
{
31
	pointer = (value == 0) ? 0 : 3;
32
}
33
34
byte PasswordCart::readIO(uint16_t port, EmuTime time)
35
{
36
	byte result = peekIO(port, time);
37
	pointer = byte(std::min(3, pointer + 1));
38
	return result;
39
}
40
41
byte PasswordCart::peekIO(uint16_t /*port*/, EmuTime /*time*/) const
42
{
43
	switch (pointer) {
44
	case 0:
45
		return 0xAA;
46
	case 1:
47
		return narrow_cast<byte>(password >> 8);
48
	case 2:
49
		return narrow_cast<byte>(password & 0xFF);
50
	default:
51
		return 0xFF;
52
	}
53
}
54
55
template<typename Archive>
56
void PasswordCart::serialize(Archive& ar, unsigned /*version*/)
57
{
58
	ar.template serializeBase<MSXDevice>(*this);
59
	ar.serialize("pointer", pointer);
60
}
61
INSTANTIATE_SERIALIZE_METHODS(PasswordCart);
62
REGISTER_MSXDEVICE(PasswordCart, "PasswordCart");
63
64
} // namespace openmsx