summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/spi_sdcard.h
blob: 4bd79e5aa3084e4b322d1955cc63064bb73b463e (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
// license:BSD-3-Clause
// copyright-holders:R. Belmont
#ifndef MAME_MACHINE_SPI_SDCARD_H
#define MAME_MACHINE_SPI_SDCARD_H

#pragma once

#include "imagedev/harddriv.h"

class spi_sdcard_device : public device_t
{
public:
	spi_sdcard_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);

	// SPI 4-wire interface
	auto spi_miso_callback() { return write_miso.bind(); }
	void spi_clock_w(int state);
	void spi_ss_w(int state) { m_ss = state; }
	void spi_mosi_w(int state) { m_in_bit = state; }

	bool get_card_present() { return !(m_harddisk == nullptr); }

	devcb_write_line write_miso;

protected:
	spi_sdcard_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock);

	virtual void device_start() override;
	virtual void device_reset() override;
	virtual void device_add_mconfig(machine_config &config) override;

	required_device<harddisk_image_device> m_image;

private:
	enum
	{
		SD_STATE_IDLE = 0,
		SD_STATE_WRITE_WAITFE,
		SD_STATE_WRITE_DATA
	};

	void send_data(int count);
	void do_command();

	u8 m_data[520], m_cmd[6];
	hard_disk_file *m_harddisk;

	u8 m_in_latch, m_out_latch;
	int m_cmd_ptr, m_state, m_out_ptr, m_out_count, m_ss, m_in_bit, m_cur_bit, m_write_ptr;
	bool m_bACMD;
};

DECLARE_DEVICE_TYPE(SPI_SDCARD, spi_sdcard_device)

#endif // MAME_MACHINE_SPI_SDCARD_H