summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/drivers/apple1.cpp
blob: 5cf5e9c3e95f4c0f0773254fb465e4bfe9ff40e9 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// license:BSD-3-Clause
// copyright-holders:R. Belmont
/***************************************************************************

    apple1.cpp - Apple I

    Next generation driver written in February 2016 by R. Belmont.
    Thanks to the original crew.

    Apple I has:
        6502 @ 1.023 MHz (~0.960 MHz with RAM refresh)
        4 or 8 KB RAM on-board
        256 byte Monitor ROM
        No IRQs, no sound, dumb terminal video
        6820 PIA for keyboard / terminal interface

    -------------------------------------------------------------------

    How to use cassettes:
    The system has no error checking or checksums, and the cassette
    has no header.
    Therefore, you must know the details, and pass these to the
    interface yourself.

    BASIC has no cassette handling. You must enter the monitor
    with: CALL -151
    then when finished, re-enter BASIC with: E2B3R

    Examples:

    A machine-language program will typically be like this:
    C100R    (enter the interface)
    0300.0FFFR  (enter the load and end addresses, then load the tape)
    You start the tape.
    When the prompt returns you stop the tape.
    0300R  (run your program)


    To Load Tape Basic:
    C100R
    E000.EFFFR
    You start the tape.
    When the prompt returns you stop the tape.
    E000R  (It must say 4C - if not, your tape is no good).
    The BASIC prompt will appear
    >@


    A BASIC program is split into two areas, one for the scratch pad,
    and one for the program proper.
    In BASIC you may have to adjust the allowed memory area, such as
    LOMEM = 768
    Then, go to the monitor: CALL -151
    C100R    (enter the interface)
    00A4.00FFR 0300.0FFFR   (load the 2 parts)
    You start the tape.
    When the prompt returns you stop the tape.
    E2B3R    (back to BASIC)
    You can LIST or RUN now.


    Saving is almost the same, when you specify the address range, enter
    W instead of R. The difficulty is finding out how long your program is.

    Insert a blank tape
    C100R
    0300.0FFFW
    Quickly press Record.
    When the prompt returns, press Stop.

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

#include "emu.h"
#include "cpu/m6502/m6502.h"
#include "machine/6821pia.h"
#include "imagedev/snapquik.h"
#include "machine/ram.h"

#include "bus/a1bus/a1bus.h"
#include "bus/a1bus/a1cassette.h"
#include "bus/a1bus/a1cffa.h"

#include "emupal.h"
#include "screen.h"
#include "softlist.h"

#define A1_CPU_TAG  "maincpu"
#define A1_PIA_TAG  "pia6821"
#define A1_BUS_TAG  "a1bus"
#define A1_BASICRAM_TAG "basicram"

class apple1_state : public driver_device
{
public:
	apple1_state(const machine_config &mconfig, device_type type, const char *tag)
		: driver_device(mconfig, type, tag),
		m_maincpu(*this, A1_CPU_TAG),
		m_pia(*this, A1_PIA_TAG),
		m_ram(*this, RAM_TAG),
		m_screen(*this, "screen"),
		m_basicram(*this, A1_BASICRAM_TAG),
		m_kb0(*this, "KEY0"),
		m_kb1(*this, "KEY1"),
		m_kb2(*this, "KEY2"),
		m_kb3(*this, "KEY3"),
		m_kbspecial(*this, "KBSPECIAL")
	{ }

	void apple1(machine_config &config);

protected:
	virtual void machine_start() override;
	virtual void machine_reset() override;

private:
	required_device<cpu_device> m_maincpu;
	required_device<pia6821_device> m_pia;
	required_device<ram_device> m_ram;
	required_device<screen_device> m_screen;
	required_shared_ptr<uint8_t> m_basicram;
	required_ioport m_kb0, m_kb1, m_kb2, m_kb3, m_kbspecial;

	uint8_t *m_ram_ptr, *m_char_ptr;
	int m_ram_size, m_char_size;

	uint8_t m_vram[40*24];
	int m_cursx, m_cursy;

	bool m_reset_down;
	bool m_clear_down;

	uint8_t m_transchar;
	uint16_t m_lastports[4];

	emu_timer *m_ready_start_timer, *m_ready_end_timer, *m_kbd_strobe_timer;

	uint32_t screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect);

	uint8_t ram_r(offs_t offset);
	void ram_w(offs_t offset, uint8_t data);
	uint8_t pia_keyboard_r();
	void pia_display_w(uint8_t data);
	DECLARE_WRITE_LINE_MEMBER(pia_display_gate_w);
	DECLARE_SNAPSHOT_LOAD_MEMBER(snapshot_cb);
	TIMER_CALLBACK_MEMBER(ready_start_cb);
	TIMER_CALLBACK_MEMBER(ready_end_cb);
	TIMER_CALLBACK_MEMBER(keyboard_strobe_cb);

	void apple1_map(address_map &map);

	void plot_text_character(bitmap_ind16 &bitmap, int xpos, int ypos, int xscale, uint32_t code, const uint8_t *textgfx_data, uint32_t textgfx_datalen);
	void poll_keyboard();
};

static const uint8_t apple1_keymap[] =
{
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '=', '[', ']', ';', '\'',    // KEY0
	',', '.', '/', '\\', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',    // KEY1
	'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\r', '_',    // KEY2
	' ', '\x1b', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                              // KEY3

	')', '!', '@', '#', '$', '%', '^', '&', '*', '(', '_', '+', '[', ']', ':', '"',     // KEY0 + shift
	'<', '>', '?', '\\', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',    // KEY1 + shift
	'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\r', '_',    // KEY2 + shift
	' ', '\x1b', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                              // KEY3 + shift

	'0', '1', '\x00', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', '8', '9', '\x1f', '=', '\x1b', '\x1d', ';', '\'', // KEY0 + CTRL
	',', '.', '/', '\x1c', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0a', '\x0b', '\x0c',  // KEY1 + CTRL
	'\x0d', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\r', '_',  // KEY2 + CTRL
	' ', '\x1b', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                              // KEY3 + CTRL

};

// header is "LOAD:abcdDATA:" where abcd is the starting address
SNAPSHOT_LOAD_MEMBER(apple1_state::snapshot_cb)
{
	uint64_t snapsize;
	uint8_t *data;
	uint16_t start, end;
	static const char hd1[6] = "LOAD:";
	static const char hd2[6] = "DATA:";

	// get the snapshot's size
	snapsize = image.length();

	if (snapsize < 12)
	{
		logerror("Snapshot is too short\n");
		return image_init_result::FAIL;
	}

	if ((snapsize - 12) > 65535)
	{
		logerror("Snapshot is too long\n");
		return image_init_result::FAIL;
	}

	data = (uint8_t *)image.ptr();
	if (!data)
	{
		logerror("Internal error loading snapshot\n");
		return image_init_result::FAIL;
	}

	if ((memcmp(hd1, data, 5)) || (memcmp(hd2, &data[7], 5)))
	{
		logerror("Snapshot is invalid\n");
		return image_init_result::FAIL;
	}

	start = (data[5]<<8) | data[6];
	end = (snapsize - 12) + start;

	// check if this fits in RAM; load below 0xe000 must fit in RAMSIZE,
	// load at 0xe000 must fit in 4K
	if (((start < 0xe000) && (end > (m_ram_size - 1))) || (end > 0xefff))
	{
		logerror("Snapshot can't fit in RAM\n");
		return image_init_result::FAIL;
	}

	if (start < 0xe000)
	{
		memcpy(m_ram_ptr + start, &data[12], snapsize - 12);
	}
	else if ((start >= 0xe000) && (start <= 0xefff))
	{
		memcpy(m_basicram + (start - 0xe000), &data[12], snapsize - 12);
	}
	else
	{
		logerror("Snapshot has invalid load address %04x\n", start);
		return image_init_result::FAIL;
	}

	return image_init_result::PASS;
}

void apple1_state::poll_keyboard()
{
	uint8_t special = m_kbspecial->read();
	uint16_t ports[4];
	int rawkey = 0;
	bool bKeypress = false;

	// handle special keys first:
	if (special & 0x10) // RESET
	{
		m_reset_down = true;
		m_maincpu->set_input_line(INPUT_LINE_RESET, ASSERT_LINE);
		m_pia->reset();
	}
	else if (m_reset_down)
	{
		m_reset_down = false;
		m_maincpu->set_input_line(INPUT_LINE_RESET, CLEAR_LINE);
	}

	if (special & 0x20) // CLEAR SCREEN
	{
		m_clear_down = true;
		memset(m_vram, 0, sizeof(m_vram));
		m_cursx = m_cursy = 0;
	}
	else
	{
		m_clear_down = false;
	}

	// lower the keyboard strobe
	m_pia->ca1_w(0);

	// cache all the rows
	ports[0] = m_kb0->read();
	ports[1] = m_kb1->read();
	ports[2] = m_kb2->read();
	ports[3] = m_kb3->read();

	for (int port = 0; port < 4; port++)
	{
		uint16_t ptread = ports[port] ^ m_lastports[port];

		for (int bit = 0; bit < 16; bit++)
		{
			// key changed?
			if (ptread & (1 << bit))
			{
				// key down?
				if (ports[port] & (1 << bit))
				{
					rawkey = (port * 16) + bit;
					m_lastports[port] |= (1 << bit);
					port = 4;   // force outer for loop to quit too
					bKeypress = true;
				}
				else    // key up
				{
					m_lastports[port] &= ~(1 << bit);
				}
				break;
			}
		}
	}

	if (bKeypress)
	{
		if ((special & 0xc) != 0)
		{
			m_transchar = apple1_keymap[rawkey + (8*16)];
		}
		else if ((special & 0x3) != 0)
		{
			m_transchar = apple1_keymap[rawkey + (4*16)];
		}
		else
		{
			m_transchar = apple1_keymap[rawkey];
		}
		// pulse the strobe line
		m_pia->ca1_w(1);
	}
}

void apple1_state::plot_text_character(bitmap_ind16 &bitmap, int xpos, int ypos, int xscale, uint32_t code,
	const uint8_t *textgfx_data, uint32_t textgfx_datalen)
{
	int x, y, i;
	const uint8_t *chardata;
	uint16_t color;
	int fg = 1, bg = 0;
	int charcode = (code & 0x1f) | (((code ^ 0x40) & 0x40) >> 1);

	/* look up the character data */
	chardata = &textgfx_data[(charcode * 8)];

	for (y = 0; y < 8; y++)
	{
		for (x = 0; x < 7; x++)
		{
			color = (chardata[y] & (1 << (6-x))) ? fg : bg;

			for (i = 0; i < xscale; i++)
			{
				bitmap.pix16(ypos + y, xpos + (x * xscale) + i) = color;
			}
		}
	}
}

uint32_t apple1_state::screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	int vramad;
	int cursor_blink = 0;
	uint8_t curs_save = 0;

	poll_keyboard();

	// the cursor 555 timer counts 0.52 of a second; the cursor is ON for
	// 2 of those counts and OFF for the last one.
	if ((int(machine().time().as_double() / (0.52 / 3.0)) % 3) < 2)
	{
		curs_save = m_vram[(m_cursy * 40) + m_cursx];
		m_vram[(m_cursy * 40) + m_cursx] = 0x40;
		cursor_blink = 1;
	}

	for (int row = 0; row < cliprect.bottom(); row += 8)
	{
		for (int col = 0; col < 40; col++)
		{
			vramad = ((row/8) * 40) + col;

			plot_text_character(bitmap, col * 14, row, 2, m_vram[vramad],
				m_char_ptr, m_char_size);
		}
	}

	if (cursor_blink)
	{
		m_vram[(m_cursy * 40) + m_cursx] = curs_save;
	}

	return 0;
}

void apple1_state::machine_start()
{
	m_ram_ptr = m_ram->pointer();
	m_ram_size = m_ram->size();
	m_char_ptr = memregion("gfx1")->base();
	m_char_size = memregion("gfx1")->bytes();

	m_reset_down = m_clear_down = false;

	m_ready_start_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(apple1_state::ready_start_cb), this));
	m_ready_end_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(apple1_state::ready_end_cb), this));
	m_kbd_strobe_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(apple1_state::keyboard_strobe_cb), this));

	// setup save states
	save_item(NAME(m_vram));
	save_item(NAME(m_cursx));
	save_item(NAME(m_cursy));
	save_item(NAME(m_reset_down));
	save_item(NAME(m_clear_down));
	save_item(NAME(m_transchar));
	save_item(NAME(m_lastports));
}

void apple1_state::machine_reset()
{
	memset(m_vram, 0, sizeof(m_vram));
	m_transchar = 0;
	m_cursx = m_cursy = 0;
	m_lastports[0] = m_lastports[1] = m_lastports[2] = m_lastports[3] = 0;
}

uint8_t apple1_state::ram_r(offs_t offset)
{
	if (offset < m_ram_size)
	{
		return m_ram_ptr[offset];
	}

	return 0xff;
}

void apple1_state::ram_w(offs_t offset, uint8_t data)
{
	if (offset < m_ram_size)
	{
		m_ram_ptr[offset] = data;
	}
}

void apple1_state::apple1_map(address_map &map)
{
	map(0x0000, 0xbfff).rw(FUNC(apple1_state::ram_r), FUNC(apple1_state::ram_w));
	map(0xd010, 0xd013).mirror(0x0fec).rw(m_pia, FUNC(pia6821_device::read), FUNC(pia6821_device::write));
	map(0xe000, 0xefff).ram().share(A1_BASICRAM_TAG);
	map(0xff00, 0xffff).rom().region(A1_CPU_TAG, 0);
}

uint8_t apple1_state::pia_keyboard_r()
{
	return m_transchar | 0x80;  // bit 7 is wired high, similar-ish to the Apple II
}

void apple1_state::pia_display_w(uint8_t data)
{
	data &= 0x7f;   // D7 is ignored by the video h/w

	// ignore characters if CLEAR is down
	if (m_clear_down)
	{
		return;
	}

	// video h/w rejects control characters except CR
	if ((data < 32) && (data != '\r'))
	{
		return;
	}

	if (data == '\r')
	{
		m_cursx = 0;
		m_cursy++;
	}
	else
	{
		m_vram[(m_cursy * 40) + m_cursx] = data;

		m_cursx++;
		if (m_cursx > 39)
		{
			m_cursx = 0;
			m_cursy++;
		}
	}

	// scroll the screen if we're at the bottom
	if (m_cursy > 23)
	{
		for (int sy = 0; sy < 23; sy++)
		{
			memcpy(&m_vram[sy * 40], &m_vram[(sy + 1) * 40], 40);
		}
		memset(&m_vram[23*40], 0, 40);
		m_cursy = 23;
	}
}

// CB2 here is connected two places: Port B bit 7 for CPU readback,
// and to the display hardware
WRITE_LINE_MEMBER(apple1_state::pia_display_gate_w)
{
	m_pia->portb_w((state << 7) ^ 0x80);

	// falling edge means start the display timer
	if (state == CLEAR_LINE)
	{
		m_ready_start_timer->adjust(m_screen->time_until_pos(m_cursy, m_cursx));
	}
}

TIMER_CALLBACK_MEMBER(apple1_state::ready_start_cb)
{
	// we're ready, pulse CB1 for 3500 nanoseconds
	m_pia->cb1_w(0);
	m_ready_end_timer->adjust(attotime::from_nsec(3500));
}

TIMER_CALLBACK_MEMBER(apple1_state::ready_end_cb)
{
	m_pia->cb1_w(1);
}

TIMER_CALLBACK_MEMBER(apple1_state::keyboard_strobe_cb)
{
	m_pia->ca1_w(0);
}

static INPUT_PORTS_START( apple1 )
	PORT_START("KEY0")
	PORT_BIT( 0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_0)        PORT_CHAR('0') PORT_CHAR(')')
	PORT_BIT( 0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_1)        PORT_CHAR('1') PORT_CHAR('!')
	PORT_BIT( 0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_2)        PORT_CHAR('2') PORT_CHAR('@')
	PORT_BIT( 0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_3)        PORT_CHAR('3') PORT_CHAR('#')
	PORT_BIT( 0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_4)        PORT_CHAR('4') PORT_CHAR('$')
	PORT_BIT( 0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_5)        PORT_CHAR('5') PORT_CHAR('%')
	PORT_BIT( 0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_6)        PORT_CHAR('6') PORT_CHAR('^')
	PORT_BIT( 0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_7)        PORT_CHAR('7') PORT_CHAR('&')
	PORT_BIT( 0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_8)        PORT_CHAR('8') PORT_CHAR('*')
	PORT_BIT( 0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_9)        PORT_CHAR('9') PORT_CHAR('(')
	PORT_BIT( 0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_MINUS)    PORT_CHAR('-') PORT_CHAR('_')
	PORT_BIT( 0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_EQUALS)   PORT_CHAR('=') PORT_CHAR('+')
	PORT_BIT( 0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_OPENBRACE) PORT_CHAR('[')
	PORT_BIT( 0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_CLOSEBRACE) PORT_CHAR(']')
	PORT_BIT( 0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_COLON)    PORT_CHAR(';') PORT_CHAR(':')
	PORT_BIT( 0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_QUOTE)    PORT_CHAR('\'') PORT_CHAR('"')

	PORT_START("KEY1")
	PORT_BIT( 0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_COMMA)    PORT_CHAR(',') PORT_CHAR('<')
	PORT_BIT( 0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_STOP)     PORT_CHAR('.') PORT_CHAR('>')
	PORT_BIT( 0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_SLASH)    PORT_CHAR('/') PORT_CHAR('?')
	PORT_BIT( 0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_BACKSLASH) PORT_CHAR('\\')
	PORT_BIT( 0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_A)        PORT_CHAR('A')
	PORT_BIT( 0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_B)        PORT_CHAR('B')
	PORT_BIT( 0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_C)        PORT_CHAR('C')
	PORT_BIT( 0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_D)        PORT_CHAR('D')
	PORT_BIT( 0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_E)        PORT_CHAR('E')
	PORT_BIT( 0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_F)        PORT_CHAR('F')
	PORT_BIT( 0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_G)        PORT_CHAR('G')
	PORT_BIT( 0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_H)        PORT_CHAR('H')
	PORT_BIT( 0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_I)        PORT_CHAR('I')
	PORT_BIT( 0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_J)        PORT_CHAR('J')
	PORT_BIT( 0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_K)        PORT_CHAR('K')
	PORT_BIT( 0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_L)        PORT_CHAR('L')

	PORT_START("KEY2")
	PORT_BIT( 0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_M)        PORT_CHAR('M')
	PORT_BIT( 0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_N)        PORT_CHAR('N')
	PORT_BIT( 0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_O)        PORT_CHAR('O')
	PORT_BIT( 0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_P)        PORT_CHAR('P')
	PORT_BIT( 0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_Q)        PORT_CHAR('Q')
	PORT_BIT( 0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_R)        PORT_CHAR('R')
	PORT_BIT( 0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_S)        PORT_CHAR('S')
	PORT_BIT( 0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_T)        PORT_CHAR('T')
	PORT_BIT( 0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_U)        PORT_CHAR('U')
	PORT_BIT( 0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_V)        PORT_CHAR('V')
	PORT_BIT( 0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_W)        PORT_CHAR('W')
	PORT_BIT( 0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_X)        PORT_CHAR('X')
	PORT_BIT( 0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_Y)        PORT_CHAR('Y')
	PORT_BIT( 0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_Z)        PORT_CHAR('Z')
	PORT_BIT( 0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_ENTER)    PORT_CHAR(13)
	PORT_BIT( 0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Backspace") PORT_CODE(KEYCODE_BACKSPACE) PORT_CHAR('_')

	PORT_START("KEY3")
	PORT_BIT( 0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_SPACE) PORT_CHAR(' ')
	PORT_BIT( 0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Escape") PORT_CODE(KEYCODE_ESC) PORT_CHAR(UCHAR_MAMEKEY(ESC))

	PORT_START("KBSPECIAL")
	PORT_BIT( 0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Left Shift") PORT_CODE(KEYCODE_LSHIFT) PORT_CHAR(UCHAR_SHIFT_1)
	PORT_BIT( 0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Right Shift") PORT_CODE(KEYCODE_RSHIFT) PORT_CHAR(UCHAR_SHIFT_1)
	PORT_BIT( 0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Left Control") PORT_CODE(KEYCODE_LCONTROL) PORT_CHAR(UCHAR_SHIFT_2)
	PORT_BIT( 0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Right Control") PORT_CODE(KEYCODE_RCONTROL) PORT_CHAR(UCHAR_SHIFT_2)
	PORT_BIT( 0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Reset") PORT_CODE(KEYCODE_F12) PORT_CHAR(UCHAR_MAMEKEY(F1))
	PORT_BIT( 0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Clear") PORT_CODE(KEYCODE_F2) PORT_CHAR(UCHAR_MAMEKEY(F2))
INPUT_PORTS_END

static void apple1_cards(device_slot_interface &device)
{
	device.option_add("cassette", A1BUS_CASSETTE);
	device.option_add("cffa", A1BUS_CFFA);
}

void apple1_state::apple1(machine_config &config)
{
	M6502(config, m_maincpu, 960000);        // effective CPU speed
	m_maincpu->set_addrmap(AS_PROGRAM, &apple1_state::apple1_map);

	// video timings are identical to the Apple II, unsurprisingly
	SCREEN(config, m_screen, SCREEN_TYPE_RASTER);
	m_screen->set_raw(XTAL(14'318'181), (65*7)*2, 0, (40*7)*2, 262, 0, 192);
	m_screen->set_screen_update(FUNC(apple1_state::screen_update));
	m_screen->set_palette("palette");

	PALETTE(config, "palette", palette_device::MONOCHROME);

	PIA6821(config, m_pia, 0);
	m_pia->readpa_handler().set(FUNC(apple1_state::pia_keyboard_r));
	m_pia->writepb_handler().set(FUNC(apple1_state::pia_display_w));
	m_pia->cb2_handler().set(FUNC(apple1_state::pia_display_gate_w));

	A1BUS(config, A1_BUS_TAG, 0).set_space(m_maincpu, AS_PROGRAM);
	A1BUS_SLOT(config, "exp", 0, A1_BUS_TAG, apple1_cards, "cassette");

	SNAPSHOT(config, "snapshot", "snp").set_load_callback(FUNC(apple1_state::snapshot_cb));

	SOFTWARE_LIST(config, "cass_list").set_original("apple1");

	RAM(config, RAM_TAG).set_default_size("48K").set_extra_options("4K,8K,12K,16K,20K,24K,28K,32K,36K,40K,44K");
}

ROM_START(apple1)
	ROM_REGION(0x100, A1_CPU_TAG, 0)
	ROM_LOAD_NIB_HIGH("apple-a2.a2", 0x0000, 0x0100, CRC(254bfb95) SHA1(b6468b72295b7d8ac288d104d252f24de1f1d611) )
	ROM_LOAD_NIB_LOW("apple-a1.a1", 0x0000, 0x0100, CRC(434f8ce6) SHA1(9deee2d39903209b20c3fc6b58e16372f8efece1) )
	ROM_REGION(0x0200, "gfx1",0)
	ROM_LOAD("s2513.d2", 0x0000, 0x0200, CRC(a7e567fc) SHA1(b18aae0a2d4f92f5a7e22640719bbc4652f3f4ee)) // apple1.vid
ROM_END

/*    YEAR  NAME    PARENT  COMPAT  MACHINE  INPUT   CLASS         INIT        COMPANY           FULLNAME */
COMP( 1976, apple1, 0,      0,      apple1,  apple1, apple1_state, empty_init, "Apple Computer", "Apple I", MACHINE_NO_SOUND_HW | MACHINE_SUPPORTS_SAVE )