summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/bbc/cart/mastersd.cpp
blob: 4395b9002ae77a0aa78c9f531b64afdfdc701f89 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// license:BSD-3-Clause
// copyright-holders:Nigel Barnes
/**********************************************************************

    MasterSD BBC Master SD Cartridge

    http://ramtop-retro.uk/mastersd.html

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


#include "emu.h"
#include "mastersd.h"


//**************************************************************************
//  DEVICE DEFINITIONS
//**************************************************************************

DEFINE_DEVICE_TYPE(BBC_MASTERSD, bbc_mastersd_device, "bbc_mastersd", "MasterSD BBC Master SD Cartridge")


//-------------------------------------------------
//  device_add_mconfig - add device configuration
//-------------------------------------------------

void bbc_mastersd_device::device_add_mconfig(machine_config &config)
{
	SPI_SDCARD(config, m_sdcard);
	m_sdcard->spi_miso_callback().set([this](int state) { m_in_bit = state; });
}

//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  bbc_mastersd_device - constructor
//-------------------------------------------------

bbc_mastersd_device::bbc_mastersd_device(const machine_config &mconfig, const char *tag, device_t *owner, const XTAL &clock)
	: device_t(mconfig, BBC_MASTERSD, tag, owner, clock)
	, device_bbc_cart_interface(mconfig, *this)
	, m_sdcard(*this, "sdcard")
	, m_spi_clock_state(false)
	, m_spi_clock_sysclk(false)
	, m_spi_clock_cycles(0)
{
}

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

void bbc_mastersd_device::device_start()
{
	m_ram = make_unique_clear<uint8_t[]>(0x8000);

	m_spi_clock = timer_alloc(FUNC(bbc_mastersd_device::spi_clock), this);

	save_pointer(NAME(m_ram), 0x8000);
	save_item(NAME(m_spi_clock_state));
	save_item(NAME(m_spi_clock_sysclk));
	save_item(NAME(m_spi_clock_cycles));
	save_item(NAME(m_in_bit));
	save_item(NAME(m_in_latch));
	save_item(NAME(m_out_latch));
}

//-------------------------------------------------
//  device_reset - device-specific startup
//-------------------------------------------------

void bbc_mastersd_device::device_reset()
{
	m_spi_clock->adjust(attotime::never);
	m_spi_clock_cycles = 0;
	m_spi_clock_state = false;
}


//-------------------------------------------------
//  read - cartridge data read
//-------------------------------------------------

uint8_t bbc_mastersd_device::read(offs_t offset, int infc, int infd, int romqa, int oe, int oe2)
{
	uint8_t data = 0xff;

	if (oe)
	{
		if (offset >= 0x3600 || romqa == 1)
		{
			data = m_ram[(offset & 0x3fff) | (romqa << 14)];
		}
		else if (offset == 0x35fe) // SPI controller data port
		{
			data = m_in_latch;
		}
		else if (offset == 0x35ff) // SPI controller status register
		{
			data = m_spi_clock_cycles > 0 ? 0x01 : 0x00;
		}
		else
		{
			data = m_rom[offset & 0x3fff];
		}
	}

	return data;
}

//-------------------------------------------------
//  write - cartridge data write
//-------------------------------------------------

void bbc_mastersd_device::write(offs_t offset, uint8_t data, int infc, int infd, int romqa, int oe, int oe2)
{
	if (oe)
	{
		if (offset >= 0x3600 || romqa == 1)
		{
			m_ram[(offset & 0x3fff) | (romqa << 14)] = data;
		}
		else if (offset == 0x35fe) // SPI controller data port
		{
			m_out_latch = data;
			m_spi_clock_cycles = 8;

			if (m_spi_clock_sysclk) // TODO: confirm fast/slow clock and dividers
				m_spi_clock->adjust(attotime::from_hz(16_MHz_XTAL / 2), 0, attotime::from_hz(16_MHz_XTAL / 2));
			else
				m_spi_clock->adjust(attotime::from_hz(16_MHz_XTAL / 32), 0, attotime::from_hz(16_MHz_XTAL / 32));
		}
		else if (offset == 0x35ff) // SPI controller clock register
		{
			m_spi_clock_sysclk = bool(BIT(data, 0));
		}
	}
}


TIMER_CALLBACK_MEMBER(bbc_mastersd_device::spi_clock)
{
	if (m_spi_clock_cycles > 0)
	{
		m_sdcard->spi_ss_w(1);

		if (m_spi_clock_state)
		{
			m_in_latch <<= 1;
			m_in_latch &= ~0x01;
			m_in_latch |= m_in_bit;

			m_sdcard->spi_clock_w(1);

			m_spi_clock_cycles--;
		}
		else
		{
			m_sdcard->spi_mosi_w(BIT(m_out_latch, 7));
			m_sdcard->spi_clock_w(0);

			m_out_latch <<= 1;
		}

		m_spi_clock_state = !m_spi_clock_state;
	}
	else
	{
		m_spi_clock_state = false;
		m_spi_clock->adjust(attotime::never);
	}
}