summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/a2bus/computereyes2.cpp
blob: 4247f2f38dfb9ade598b9dc863706ab045dd8d83 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// license:BSD-3-Clause
// copyright-holders:R. Belmont
/*********************************************************************

    computereyes2.c

    Implemention of the Digital Vision ComputerEyes/2 card.

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

#include "emu.h"
#include "computereyes2.h"

/***************************************************************************
    PARAMETERS
***************************************************************************/

//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

DEFINE_DEVICE_TYPE(A2BUS_COMPUTEREYES2, a2bus_computereyes2_device, "a2ceyes2", "Digital Vision ComputerEyes/2")

/***************************************************************************
    FUNCTION PROTOTYPES
***************************************************************************/

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

void a2bus_computereyes2_device::device_add_mconfig(machine_config &config)
{
	IMAGE_PICTURE(config, m_picture);
}

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

a2bus_computereyes2_device::a2bus_computereyes2_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock) :
	device_t(mconfig, type, tag, owner, clock),
	device_a2bus_card_interface(mconfig, *this),
	m_picture(*this, "srcimg")
{
}

a2bus_computereyes2_device::a2bus_computereyes2_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	a2bus_computereyes2_device(mconfig, A2BUS_COMPUTEREYES2, tag, owner, clock)
{
}

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

void a2bus_computereyes2_device::device_start()
{
	save_item(NAME(m_x));
	save_item(NAME(m_y));
	save_item(NAME(m_cer0));
	save_item(NAME(m_cer1));
	save_item(NAME(m_cer2));
	save_item(NAME(m_threshold));
	save_item(NAME(m_bActive));
	save_item(NAME(m_a2_bitmap));
}

void a2bus_computereyes2_device::device_reset()
{
	m_x = m_y = 0;
	m_cer0 = m_cer2 = 0;
	m_cer1 = 0x80;  // interface disabled
}


/*-------------------------------------------------
    read_c0nx - called for reads from this card's c0nx space

    CER0 read bits
    01 = capture data bit (read)
    10 = sync (read)
    20 = test bit (used in detection) (read)

   CER0 write bits
    0F = brightness (write)
    40 = monitor relay (write)
    80 = turn on test bit (write)

   CER1 write bits
    0F = contrast (guessing bits 0..3?)
    80 = interface on (bit 7)

   CER2 write bits
   (any) resets the threshold for a new pixel
-------------------------------------------------*/

uint8_t a2bus_computereyes2_device::read_c0nx(uint8_t offset)
{
	switch (offset)
	{
		case 0:
			if (m_cer0 & 0x80)
			{
				return m_cer0 | 0x20;
			}
			else
			{
				u8 ret = (m_cer0 & 0xc0) | 0x10;
				u64 video_ticks = machine().time().as_ticks(1021800);
				int frame_time = video_ticks % (65 * 262);  // 65 clocks per scanline, 262 scanlines per frame
				int h = frame_time % 65;
				int v = frame_time / 65;

				// generate sync bit
				if (v > 224)
				{
					// vsync
					ret &= ~0x10;
				}
				else if (h > 49)
				{
					// hsync
					ret &= ~0x10;
				}

				// is the interface enabled?  (Active low, 0 = enabled)
				if (!(m_cer1 & 0x80))
				{
					{
						if (m_bActive)
						{
							//printf("read at X=%d Y=%d CX=%d CY=%d thr=%02x\n", h, v, m_x, m_y, m_threshold);
							ret |= (m_a2_bitmap[m_x + ((v-2) * 280)] > m_threshold) ? 0 : 1;
							if (m_threshold > 0x20)
							{
								m_threshold -= 0x20;
							}
							else
							{
								m_y++;
								if (m_y >= 192)
								{
									if (m_x < 280)
									{
										m_x++;
									}
									else
									{
										m_x = 0;
									}

									m_bActive = false;
								}
							}
						}
					/*  else
					    {
					        printf("SYNC at X=%d Y=%d CX=%d CY=%d thr=%02x\n", h, v, m_x, m_y, m_threshold);
					    }*/
					}
				}
				return ret;
			}
			break;

		case 1: return m_cer1;
		case 2: return m_cer2;
	}

	return 0;
}


/*-------------------------------------------------
    write_c0nx - called for writes to this card's c0nx space
-------------------------------------------------*/

void a2bus_computereyes2_device::write_c0nx(uint8_t offset, uint8_t data)
{
	switch (offset)
	{
		case 0:
			m_cer0 = data & 0xcf;
			break;

		case 1:
			if (!(data & 0x80))
			{
				m_bActive = false;
			}
			m_y = 0;

			if (!(data & 0x80) && (m_cer1 & 0x80))
			{
				m_x = m_y = 0;
				std::fill_n(m_a2_bitmap, 280*193, 0);

				m_bitmap = m_picture->get_bitmap();
				if (m_bitmap)
				{
					// convert arbitrary sized ARGB32 image to a 188x193 image with 256 levels of grayscale
					double stepx = (double)m_bitmap->width() / 188.0;
					double stepy = (double)m_bitmap->height() / 193.0;

					for (int y = 0; y < 193; y++)
					{
						for (int x = 0; x < 280; x++)
						{
							u32 pixel = m_bitmap->pix((int)((double)y * stepy), (int)((double)x * stepx));
							double mono = ((0.2126 * (double)(((pixel>>16) & 0xff) / 255.0)) +
								   (0.7152 * (double)(((pixel>>8) & 0xff) / 255.0)) +
								   (0.0722 * (double)((pixel& 0xff) / 255.0)));
							m_a2_bitmap[(y*280)+x] = (u8)(mono * 255.0);
						}
					}
				}
			}
			m_cer1 = data;
			break;

		case 2: // written to initialize a sample
			m_cer2 = data;
			m_threshold = 0xe0;
			if (!m_bActive)
			{
				m_y = 0;
			}
			m_bActive = true;
			break;
	}
}