summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/nes_ctrl/ctrl.h
blob: e424913099bb3c82aa09ceca32b61a00c1a16f19 (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
// license:BSD-3-Clause
// copyright-holders:Fabio Priuli
/**********************************************************************

    Nintendo Family Computer & Entertainment System controller port
    emulation

**********************************************************************

    Known Issues:
    - Currently the FC expansion port is emulated as a control port

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

#ifndef MAME_BUS_NES_CTRL_CTRL_H
#define MAME_BUS_NES_CTRL_CTRL_H

#pragma once

#include "screen.h"


//**************************************************************************
//  TYPE DEFINITIONS
//**************************************************************************

class nes_control_port_device;

// ======================> device_nes_control_port_interface

class device_nes_control_port_interface : public device_interface
{
public:
	// construction/destruction
	virtual ~device_nes_control_port_interface();

	virtual u8 read_bit0() { return 0; }
	virtual u8 read_bit2() { return 0; } // intended only for P2 microphone
	virtual u8 read_bit34() { return 0; }
	virtual u8 read_exp(offs_t offset) { return 0; }
	virtual void write(u8 data) { }

protected:
	device_nes_control_port_interface(const machine_config &mconfig, device_t &device);

	// helper to keep track of strobe bit, returns true on 1 to 0 transitions
	bool write_strobe(u8 data) { u8 prev = m_strobe; m_strobe = data & 1; return prev && !m_strobe; }

	nes_control_port_device *m_port;
	u8 m_strobe;
};


// ======================> nes_control_port_device

class nes_control_port_device : public device_t,
								public device_single_card_slot_interface<device_nes_control_port_interface>
{
public:
	// construction/destruction
	template <typename T>
	nes_control_port_device(machine_config const &mconfig, char const *tag, device_t *owner, T &&opts, char const *dflt)
		: nes_control_port_device(mconfig, tag, owner)
	{
		option_reset();
		opts(*this);
		set_default_option(dflt);
		set_fixed(false);
	}
	nes_control_port_device(const machine_config &mconfig, const char *tag, device_t *owner, const XTAL &clock = XTAL());
	virtual ~nes_control_port_device();

	u8 read_bit0();
	u8 read_bit2();
	u8 read_bit34();
	u8 read_exp(offs_t offset);
	void write(u8 data);
	template <typename T> void set_screen_tag(T &&tag) { m_screen.set_tag(std::forward<T>(tag)); }

	// for peripherals that interact with the machine's screen
	required_device<screen_device> m_screen;

protected:
	// device-level overrides
	virtual void device_start() override;

	// devices
	device_nes_control_port_interface *m_device;
};


// device type definition
DECLARE_DEVICE_TYPE(NES_CONTROL_PORT, nes_control_port_device)

void nes_control_port1_devices(device_slot_interface &device);
void nes_control_port2_devices(device_slot_interface &device);
void nes_control_special_devices(device_slot_interface &device);
void fc_control_port1_devices(device_slot_interface &device);
void fc_control_port2_devices(device_slot_interface &device);
void fc_expansion_devices(device_slot_interface &device);
void famibox_control_port12_devices(device_slot_interface &device);
void famibox_control_port3_devices(device_slot_interface &device);

#endif // MAME_BUS_NES_CTRL_CTRL_H