1 // license:BSD-3-Clause
2 // copyright-holders:David Haywood
3 
4 // has separate input / output addresses but still uses direction registers, or I've misunderstood this entirely.
5 
6 #include "emu.h"
7 #include "xavix2002_io.h"
8 
9 #define VERBOSE 0
10 #include "logmacro.h"
11 
12 DEFINE_DEVICE_TYPE(XAVIX2002IO, xavix2002_io_device, "xavix2002io", "XaviX 2002 IO")
13 
xavix2002_io_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)14 xavix2002_io_device::xavix2002_io_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
15 	: device_t(mconfig, XAVIX2002IO, tag, owner, clock)
16 	, m_in0_cb(*this)
17 	, m_in1_cb(*this)
18 	, m_in2_cb(*this)
19 	, m_out0_cb(*this)
20 	, m_out1_cb(*this)
21 	, m_out2_cb(*this)
22 {
23 }
24 
device_start()25 void xavix2002_io_device::device_start()
26 {
27 	m_in0_cb.resolve_safe(0xff);
28 	m_in1_cb.resolve_safe(0xff);
29 	m_in2_cb.resolve_safe(0xff);
30 
31 	m_out0_cb.resolve_safe();
32 	m_out1_cb.resolve_safe();
33 	m_out2_cb.resolve_safe();
34 
35 	save_item(NAME(m_sx_pio_dir));
36 	save_item(NAME(m_sx_pio_out));
37 }
38 
device_reset()39 void xavix2002_io_device::device_reset()
40 {
41 	for (int i = 0; i < 3; i++)
42 	{
43 		m_sx_pio_dir[i] = 0;
44 		m_sx_pio_out[i] = 0;
45 	}
46 }
47 
pio_dir_w(offs_t offset,uint8_t data)48 void xavix2002_io_device::pio_dir_w(offs_t offset, uint8_t data)
49 {
50 	LOG("%s: superxavix pio_dir_w (port %d) %02x\n", machine().describe_context(), offset, data);
51 
52 	if (offset < 3)
53 	{
54 		m_sx_pio_dir[offset] = data;
55 		pio_out_w(offset, m_sx_pio_out[offset]);
56 		// update port?
57 	}
58 }
59 
pio_dir_r(offs_t offset)60 uint8_t xavix2002_io_device::pio_dir_r(offs_t offset)
61 {
62 	LOG("%s: superxavix pio_dir_r (port %d)\n", machine().describe_context(), offset);
63 	uint8_t ret = 0x00;
64 
65 	if (offset < 3)
66 	{
67 		ret = m_sx_pio_dir[offset];
68 	}
69 
70 	return ret;
71 }
72 
pio_out_w(offs_t offset,uint8_t data)73 void xavix2002_io_device::pio_out_w(offs_t offset, uint8_t data)
74 {
75 	LOG("%s: superxavix pio_out_w (port %d) %02x\n", machine().describe_context(), offset, data);
76 
77 	if (offset < 3)
78 	{
79 		m_sx_pio_out[offset] = data;
80 
81 		// TODO: look at direction register
82 
83 		uint8_t outdata = m_sx_pio_out[offset] & m_sx_pio_dir[offset];
84 
85 		switch (offset)
86 		{
87 			case 0: m_out0_cb(outdata); break;
88 			case 1: m_out1_cb(outdata); break;
89 			case 2: m_out2_cb(outdata); break;
90 			default: break;
91 		}
92 	}
93 }
94 
pio_out_r(offs_t offset)95 uint8_t xavix2002_io_device::pio_out_r(offs_t offset)
96 {
97 	// what does this actually read?
98 
99 	LOG("%s: superxavix pio_out_r (port %d)\n", machine().describe_context(), offset);
100 
101 	uint8_t ret = 0x00;
102 
103 	if (offset<3)
104 		ret = m_sx_pio_out[offset];
105 
106 	return ret;
107 }
108 
109 
pio_in_r(offs_t offset)110 uint8_t xavix2002_io_device::pio_in_r(offs_t offset)
111 {
112 	LOG("%s: superxavix pio_in_r (port %d)\n", machine().describe_context(), offset);
113 
114 	uint8_t ret = 0x00;
115 
116 	switch (offset)
117 	{
118 		case 0: ret = m_in0_cb(); break;
119 		case 1: ret = m_in1_cb(); break;
120 		case 2: ret = m_in2_cb(); break;
121 		default: ret = 0x00; break;
122 	}
123 
124 	// mask with direction register before returning
125 
126 	return ret;
127 }
128