summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/pgm2_memcard.cpp
blob: 1897b90141b84ffccd93ad737de4408bb3164124 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// license:BSD-3-Clause
// copyright-holders:David Haywood, Miodrag Milanovic
/*********************************************************************

    pgm2_memcard.cpp

    PGM2 Memory card functions.
    Presumably Siemens SLE 4442 or compatible.

*********************************************************************/

#include "emu.h"
#include "emuopts.h"

#include "pgm2_memcard.h"


// device type definition
DEFINE_DEVICE_TYPE(PGM2_MEMCARD, pgm2_memcard_device, "pgm2_memcard", "PGM2 Memory Card")

//-------------------------------------------------
//  pgm2_memcard_device - constructor
//-------------------------------------------------

pgm2_memcard_device::pgm2_memcard_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, PGM2_MEMCARD, tag, owner, clock)
	, device_image_interface(mconfig, *this)
{
}


//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void pgm2_memcard_device::device_start()
{
	save_item(NAME(m_memcard_data));
}

/*-------------------------------------------------
    memcard_insert - insert an existing memory card
    with the given index
-------------------------------------------------*/

image_init_result pgm2_memcard_device::call_load()
{
	authenticated = false;
	if(length() != 0x108)
		return image_init_result::FAIL;

	fseek(0, SEEK_SET);
	size_t ret = fread(m_memcard_data, 0x100);
	if(ret != 0x100)
		return image_init_result::FAIL;
	ret = fread(m_protection_data, 4);
	if (ret != 4)
		return image_init_result::FAIL;
	ret = fread(m_security_data, 4);
	if (ret != 4)
		return image_init_result::FAIL;

	return image_init_result::PASS;
}

void pgm2_memcard_device::call_unload()
{
	authenticated = false;
	fseek(0, SEEK_SET);
	fwrite(m_memcard_data, 0x100);
	fwrite(m_protection_data, 4);
	fwrite(m_security_data, 4);
}

image_init_result pgm2_memcard_device::call_create(int format_type, util::option_resolution *format_options)
{
	authenticated = false;
	// cards must contain valid defaults for each game / region or they don't work?
	memory_region *rgn = memregion("^default_card");

	if (!rgn)
		return image_init_result::FAIL;

	memcpy(m_memcard_data, rgn->base(), 0x100);
	memcpy(m_protection_data, rgn->base() + 0x100, 4);
	memcpy(m_security_data, rgn->base() + 0x104, 4);

	size_t ret = fwrite(rgn->base(), 0x108);
	if(ret != 0x108)
		return image_init_result::FAIL;

	return image_init_result::PASS;
}

void pgm2_memcard_device::auth(uint8_t p1, uint8_t p2, uint8_t p3)
{
	if (m_security_data[0] & 7)
	{
		if (m_security_data[1] == p1 && m_security_data[2] == p2 && m_security_data[3] == p3) {
			authenticated = true;
			m_security_data[0] = 7;
		}
		else {
			authenticated = false;
			m_security_data[0] >>= 1; // hacky
			if (m_security_data[0] & 7)
				popmessage("Wrong IC Card password !!!\n");
			else
				popmessage("Wrong IC Card password, card was locked!!!\n");
		}
	}
}

READ8_MEMBER(pgm2_memcard_device::read)
{
	return m_memcard_data[offset];
}

WRITE8_MEMBER(pgm2_memcard_device::write)
{
	if (authenticated && (offset >= 0x20 || (m_protection_data[offset>>3] & (1 <<(offset & 7)))))
	{
		m_memcard_data[offset] = data;
	}
}

READ8_MEMBER(pgm2_memcard_device::read_prot)
{
	return m_protection_data[offset];
}

WRITE8_MEMBER(pgm2_memcard_device::write_prot)
{
	if (authenticated)
		m_protection_data[offset] &= data;
}

READ8_MEMBER(pgm2_memcard_device::read_sec)
{
	if (!authenticated)
		return 0xff; // guess
	return m_security_data[offset];
}

WRITE8_MEMBER(pgm2_memcard_device::write_sec)
{
	if (authenticated)
		m_security_data[offset] = data;
}