summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/zx.cpp
blob: 3f95ecbf971d202a9608c4346bc60b18b42b10c3 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// license:GPL-2.0+
// copyright Olivier Galibert, Buchmueller, Krzysztof Strzecha, Robbbert
/***************************************************************************
    zx.c

    machine driver
    Juergen Buchmueller <pullmoll@t-online.de>, Dec 1999

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

#include "emu.h"
#include "includes/zx.h"

void zx_state::init_zx()
{
	m_program = &m_maincpu->space(AS_PROGRAM);
	m_tape_input = timer_alloc(TIMER_TAPE_INPUT);

	if(m_ram->size() == 32768)
		m_program->unmap_readwrite(0x8000, 0xbfff);
	else if(m_ram->size() == 16384)
		m_program->unmap_readwrite(0x8000, 0xffff);
	else if(m_ram->size() < 16384)
		m_program->unmap_readwrite(0x4000 + m_ram->size(), 0xffff);
}

void zx_state::machine_reset()
{
	m_prev_refresh = 0xff;

	m_vsync_active = false;
	m_hsync_active = false;
	m_base_vsync_clock = 0;
	m_ypos = 0;

	m_nmi_on = false;
	m_nmi_generator_active = false;

	m_cassette_cur_level = 0;

	m_tape_input->adjust(attotime::from_hz(44100), 0, attotime::from_hz(44100));
}

void zx_state::zx_tape_input()
{
	m_cassette_cur_level = m_cassette->input();
}

void zx_state::drop_sync()
{
	if (m_vsync_active) {
		uint64_t time = m_maincpu->total_cycles();
		m_vsync_active = false;
		m_cassette->output(-1.0);

		int xs = 2*((m_vsync_start_time - m_base_vsync_clock) % 207);
		int ys = (m_vsync_start_time - m_base_vsync_clock) / 207;
		int xe = 2*((time - m_base_vsync_clock) % 207);
		int ye = (time - m_base_vsync_clock) / 207;
		if(xs >= 384) {
			xs = 0;
			ys++;
		}
		if(xe >= 384) {
			xe = 0;
			ye++;
		}
		if(ys < 311) {
			if(ye > 310) {
				ye = 311;
				xe = 0;
			}
			if(ys == ye) {
				uint16_t *dest = &m_bitmap_render->pix(ys, xs);
				for(int x = xs; x < xe; x++)
					*dest++ = 1;
			} else {
				uint16_t *dest = &m_bitmap_render->pix(ys, xs);
				for(int x = xs; x < 384; x++)
					*dest++ = 1;
				for(int y = ys+1; y < ye; y++) {
					dest = &m_bitmap_render->pix(y, 0);
					for(int x = 0; x<384; x++)
						*dest++ = 1;
				}
				dest = &m_bitmap_render->pix(ye, 0);
				for(int x = 0; x < xe; x++)
					*dest++ = 1;
			}
		}

		// Short is hsync
		if(time - m_vsync_start_time > 1000) {
			// Ignore too short frame times, they're cassette output
			if(time - m_base_vsync_clock > 52000) {
				logerror("frame time %d\n", int(time - m_base_vsync_clock));

				rectangle rect(0, 383, 0, 310);
				copybitmap(*m_bitmap_buffer, *m_bitmap_render, 0, 0, 0, 0, rect);
				m_bitmap_render->fill(0);
				m_base_vsync_clock = time;
				m_ypos = 0;
			}
			if(m_nmi_on)
				m_maincpu->set_input_line(INPUT_LINE_NMI, CLEAR_LINE);
			m_nmi_on = m_hsync_active = false;
			recalc_hsync();
		} else
			m_ypos = ((time-m_base_vsync_clock)%207) < 192 ? 0 : -1;
	}
}

uint8_t zx_state::zx80_io_r(offs_t offset)
{
	/* port FE = read keyboard, NTSC/PAL diode, and cass bit; turn off HSYNC-generator/cass-out
	    The upper 8 bits are used to select a keyboard scan line */

	uint8_t data = 0xff;

	if (!(offset & 0x01))
	{
		if ((offset & 0x0100) == 0)
			data &= m_io_row0->read();
		if ((offset & 0x0200) == 0)
			data &= m_io_row1->read();
		if ((offset & 0x0400) == 0)
			data &= m_io_row2->read();
		if ((offset & 0x0800) == 0)
			data &= m_io_row3->read();
		if ((offset & 0x1000) == 0)
			data &= m_io_row4->read();
		if ((offset & 0x2000) == 0)
			data &= m_io_row5->read();
		if ((offset & 0x4000) == 0)
			data &= m_io_row6->read();
		if ((offset & 0x8000) == 0)
			data &= m_io_row7->read();

		if (!m_io_config->read())
			data &= ~0x40;

		m_cassette->output(+1.0);

		if (!m_vsync_active && !m_nmi_generator_active) {
			m_vsync_active = true;
			m_vsync_start_time = m_maincpu->total_cycles();
		}

		if(m_cassette_cur_level <= 0)
			data &= 0x7f;
	}

	return data;
}

uint8_t zx_state::zx81_io_r(offs_t offset)
{
/* port FB = read printer status, not emulated
    FE = read keyboard, NTSC/PAL diode, and cass bit; turn off HSYNC-generator/cass-out
    The upper 8 bits are used to select a keyboard scan line */

	uint8_t data = 0xff;

	if (!(offset & 0x01))
	{
		if ((offset & 0x0100) == 0)
			data &= m_io_row0->read();
		if ((offset & 0x0200) == 0)
			data &= m_io_row1->read();
		if ((offset & 0x0400) == 0)
			data &= m_io_row2->read();
		if ((offset & 0x0800) == 0)
			data &= m_io_row3->read();
		if ((offset & 0x1000) == 0)
			data &= m_io_row4->read();
		if ((offset & 0x2000) == 0)
			data &= m_io_row5->read();
		if ((offset & 0x4000) == 0)
			data &= m_io_row6->read();
		if ((offset & 0x8000) == 0)
			data &= m_io_row7->read();

		if (!m_io_config->read())
			data &= ~0x40;

		m_cassette->output(+1.0);

		if (!m_vsync_active && !m_nmi_generator_active) {
			m_vsync_active = true;
			m_vsync_start_time = m_maincpu->total_cycles();
		}

		if(m_cassette_cur_level <= 0)
			data &= 0x7f;
	}

	return data;
}

uint8_t zx_state::pc8300_io_r(offs_t offset)
{
/* port F5 = sound
    F6 = unknown
    FB = read printer status, not emulated
    FE = read keyboard and cass bit; turn off HSYNC-generator/cass-out
    The upper 8 bits are used to select a keyboard scan line.
    No TV diode */

	uint8_t data = 0xff;
	uint8_t offs = offset & 0xff;

	if (offs == 0xf5)
	{
		m_speaker_state ^= 1;
		m_speaker->level_w(m_speaker_state);
	}
	else
	if (offs == 0xfe)
	{
		if ((offset & 0x0100) == 0)
			data &= m_io_row0->read();
		if ((offset & 0x0200) == 0)
			data &= m_io_row1->read();
		if ((offset & 0x0400) == 0)
			data &= m_io_row2->read();
		if ((offset & 0x0800) == 0)
			data &= m_io_row3->read();
		if ((offset & 0x1000) == 0)
			data &= m_io_row4->read();
		if ((offset & 0x2000) == 0)
			data &= m_io_row5->read();
		if ((offset & 0x4000) == 0)
			data &= m_io_row6->read();
		if ((offset & 0x8000) == 0)
			data &= m_io_row7->read();

		m_cassette->output(+1.0);
		if(m_cassette_cur_level <= 0)
			data &= 0x7f;
	}

	return data;
}

uint8_t zx_state::pow3000_io_r(offs_t offset)
{
/* port 7E = read NTSC/PAL diode
    F5 = sound
    F6 = unknown
    FB = read printer status, not emulated
    FE = read keyboard and cass bit; turn off HSYNC-generator/cass-out
    The upper 8 bits are used to select a keyboard scan line */

	uint8_t data = 0xff;
	uint8_t offs = offset & 0xff;

	if (offs == 0x7e)
	{
		data = (m_io_config->read());
	}
	else
	if (offs == 0xf5)
	{
		m_speaker_state ^= 1;
		m_speaker->level_w(m_speaker_state);
	}
	else
	if (offs == 0xfe)
	{
		if ((offset & 0x0100) == 0)
			data &= m_io_row0->read();
		if ((offset & 0x0200) == 0)
			data &= m_io_row1->read();
		if ((offset & 0x0400) == 0)
			data &= m_io_row2->read();
		if ((offset & 0x0800) == 0)
			data &= m_io_row3->read();
		if ((offset & 0x1000) == 0)
			data &= m_io_row4->read();
		if ((offset & 0x2000) == 0)
			data &= m_io_row5->read();
		if ((offset & 0x4000) == 0)
			data &= m_io_row6->read();
		if ((offset & 0x8000) == 0)
			data &= m_io_row7->read();

		m_cassette->output(+1.0);
		if(m_cassette_cur_level <= 0)
			data &= 0x7f;
	}

	return data;
}

void zx_state::zx80_io_w(offs_t offset, uint8_t data)
{
/* port FF = write HSYNC and cass data */

	uint8_t offs = offset & 0xff;

	if (offs == 0xff)
		m_cassette->output(-1.0);
}

void zx_state::zx81_io_w(offs_t offset, uint8_t data)
{
/* port F5 = unknown, pc8300/pow3000/lambda only
    F6 = unknown, pc8300/pow3000/lambda only
    FB = write data to printer, not emulated
    FD = turn off NMI generator
    FE = turn on NMI generator
    FF = write HSYNC and cass data */

	if (!(offset & 0x01) && !m_nmi_generator_active) {
		m_nmi_generator_active = true;
		m_nmi_on = m_hsync_active;
		m_maincpu->set_input_line(INPUT_LINE_NMI, m_nmi_on ? ASSERT_LINE : CLEAR_LINE);
		recalc_hsync();
	}

	if (!(offset & 0x02) && m_nmi_generator_active) {
		m_nmi_generator_active = false;
		if(m_nmi_on) {
			m_maincpu->set_input_line(INPUT_LINE_NMI, CLEAR_LINE);
			m_nmi_on = false;
		}
	}

	drop_sync();
}