/
apple2.cpp
1633 lines (1382 loc) · 64.8 KB
/
apple2.cpp
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// license:BSD-3-Clause
// copyright-holders:R. Belmont
/***************************************************************************
apple2.cpp - Apple II/II Plus and clones
Next generation driver written in September/October 2014 by R. Belmont.
Thanks to the original Apple II series driver's authors: Mike Balfour, Nathan Woods, and R. Belmont
Special thanks to the Apple II Documentation Project/Antoine Vignau and Peter Ferrie.
II: original base model. RAM sizes of 4, 8, 12, 16, 20, 24, 32, 36, and 48 KB possible.
8K of ROM at $E000-$FFFF, empty sockets for $D000-$D7FF and $D800-$DFFF.
Programmer's Aid #1 was sold by Apple for $D000-$D7FF, some third-party ROMs
were also available.
Revision 0 (very rare) had only 4 hi-res colors (blue and orange were missing).
Revision 0 boards also did not include a color killer in text mode, making text
fringey on color TVs/monitors.
ROM contains original non-autostart Monitor and Integer BASIC; apparently
Autostart + Integer is also possible.
II Plus: RAM options reduced to 16/32/48 KB.
ROM expanded to 12KB from $D000-$FFFF containing Applesoft BASIC and
the Autostart Monitor. Applesoft is a licensed version of Microsoft's
6502 BASIC as also found in Commodore and many other computers.
Users of both models often connected the SHIFT key to the paddle #2 button
(mapped to $C063) in order to inform properly written software that characters
were to be intended upper/lower case.
Both models commonly included a RAM "language card" in slot 0 which added 16K
of RAM which could be banked into the $D000-$FFFF space to replace the ROMs.
This allowed running Applesoft on a II and Integer BASIC on a II Plus.
A II Plus with this card installed is often called a "64K Apple II"; this is
the base configuration required to run ProDOS and some larger games.
************************************************************************/
#include "emu.h"
#include "apple2common.h"
#include "apple2video.h"
#include "bus/a2bus/a2bus.h"
#include "bus/a2bus/cards.h"
#include "bus/a2gameio/gameio.h"
#include "cpu/m6502/m6502.h"
#include "imagedev/cassette.h"
#include "machine/74259.h"
#include "machine/kb3600.h"
#include "machine/ram.h"
#include "machine/timer.h"
#include "sound/spkrdev.h"
#include "screen.h"
#include "softlist_dev.h"
#include "speaker.h"
#include "utf8.h"
namespace {
#define A2_CPU_TAG "maincpu"
#define A2_KBDC_TAG "ay3600"
#define A2_SPEAKER_TAG "speaker"
#define A2_CASSETTE_TAG "tape"
#define A2_UPPERBANK_TAG "inhbank"
#define A2_VIDEO_TAG "a2video"
class apple2_state : public driver_device
{
public:
apple2_state(const machine_config &mconfig, device_type type, const char *tag) :
driver_device(mconfig, type, tag),
m_maincpu(*this, A2_CPU_TAG),
m_screen(*this, "screen"),
m_scantimer(*this, "scantimer"),
m_ram(*this, RAM_TAG),
m_ay3600(*this, A2_KBDC_TAG),
m_video(*this, A2_VIDEO_TAG),
m_a2common(*this, "a2common"),
m_a2bus(*this, "a2bus"),
m_gameio(*this, "gameio"),
m_kbspecial(*this, "keyb_special"),
m_kbrepeat(*this, "keyb_repeat"),
m_resetdip(*this, "reset_dip"),
m_sysconfig(*this, "a2_config"),
m_speaker(*this, A2_SPEAKER_TAG),
m_cassette(*this, A2_CASSETTE_TAG),
m_softlatch(*this, "softlatch"),
m_upperbank(*this, A2_UPPERBANK_TAG)
{ }
required_device<cpu_device> m_maincpu;
required_device<screen_device> m_screen;
required_device<timer_device> m_scantimer;
required_device<ram_device> m_ram;
required_device<ay3600_device> m_ay3600;
required_device<a2_video_device_composite> m_video;
required_device<apple2_common_device> m_a2common;
required_device<a2bus_device> m_a2bus;
required_device<apple2_gameio_device> m_gameio;
required_ioport m_kbspecial;
required_ioport m_kbrepeat;
optional_ioport m_resetdip;
required_ioport m_sysconfig;
required_device<speaker_sound_device> m_speaker;
required_device<cassette_image_device> m_cassette;
required_device<addressable_latch_device> m_softlatch;
memory_view m_upperbank;
TIMER_DEVICE_CALLBACK_MEMBER(apple2_interrupt);
TIMER_DEVICE_CALLBACK_MEMBER(ay3600_repeat);
virtual void machine_start() override;
virtual void machine_reset() override;
u8 ram_r(offs_t offset);
void ram_w(offs_t offset, u8 data);
u8 keyb_data_r();
u8 keyb_strobe_r();
void keyb_strobe_w(u8 data);
u8 cassette_toggle_r();
void cassette_toggle_w(u8 data);
u8 speaker_toggle_r();
void speaker_toggle_w(u8 data);
u8 utility_strobe_r();
void utility_strobe_w(u8 data);
u8 switches_r(offs_t offset);
u8 flags_r(offs_t offset);
u8 controller_strobe_r();
void controller_strobe_w(u8 data);
u8 c080_r(offs_t offset);
void c080_w(offs_t offset, u8 data);
u8 c100_r(offs_t offset);
void c100_w(offs_t offset, u8 data);
u8 c800_r(offs_t offset);
void c800_w(offs_t offset, u8 data);
u8 inh_r(offs_t offset);
void inh_w(offs_t offset, u8 data);
void a2bus_irq_w(int state);
void a2bus_nmi_w(int state);
void a2bus_inh_w(int state);
int ay3600_shift_r();
int ay3600_control_r();
void ay3600_data_ready_w(int state);
void ay3600_ako_w(int state);
void apple2_common(machine_config &config);
void apple2jp(machine_config &config);
void apple2(machine_config &config);
void space84(machine_config &config);
[[maybe_unused]] void dodo(machine_config &config);
void albert(machine_config &config);
void ivelultr(machine_config &config);
void apple2p(machine_config &config);
void apple2_map(address_map &map);
private:
int m_speaker_state, m_cassette_state;
double m_joystick_x1_time, m_joystick_y1_time, m_joystick_x2_time, m_joystick_y2_time;
u16 m_lastchar, m_strobe;
u8 m_transchar;
bool m_anykeydown;
int m_inh_slot;
int m_cnxx_slot;
u8 *m_ram_ptr;
int m_ram_size;
int m_inh_bank;
bool m_reset_latch;
double m_x_calibration, m_y_calibration;
device_a2bus_card_interface *m_slotdevice[8];
u8 read_floatingbus();
offs_t dasm_trampoline(std::ostream &stream, offs_t pc, const util::disasm_interface::data_buffer &opcodes, const util::disasm_interface::data_buffer ¶ms);
};
/***************************************************************************
PARAMETERS
***************************************************************************/
#define JOYSTICK_DELTA 80
#define JOYSTICK_SENSITIVITY 50
#define JOYSTICK_AUTOCENTER 80
offs_t apple2_state::dasm_trampoline(std::ostream &stream, offs_t pc, const util::disasm_interface::data_buffer &opcodes, const util::disasm_interface::data_buffer ¶ms)
{
return m_a2common->dasm_override(stream, pc, opcodes, params);
}
void apple2_state::a2bus_irq_w(int state)
{
m_maincpu->set_input_line(M6502_IRQ_LINE, state);
}
void apple2_state::a2bus_nmi_w(int state)
{
m_maincpu->set_input_line(INPUT_LINE_NMI, state);
}
// This code makes a ton of assumptions because we can guarantee a pre-IIe machine!
void apple2_state::a2bus_inh_w(int state)
{
if (state == ASSERT_LINE)
{
// assume no cards are pulling /INH
m_inh_slot = -1;
// scan the slots to figure out which card(s) are INHibiting stuff
for (int i = 0; i <= 7; i++)
{
if (m_slotdevice[i])
{
// this driver only can inhibit from 0xd000-0xffff
if ((m_slotdevice[i]->inh_start() == 0xd000) &&
(m_slotdevice[i]->inh_end() == 0xffff))
{
if ((m_slotdevice[i]->inh_type() & INH_READ) == INH_READ)
{
if (m_inh_bank != 1)
{
m_upperbank.select(1);
m_inh_bank = 1;
}
}
else
{
if (m_inh_bank != 0)
{
m_upperbank.select(0);
m_inh_bank = 0;
}
}
m_inh_slot = i;
break;
}
}
}
// if no slots are inhibiting, make sure ROM is fully switched in
if ((m_inh_slot == -1) && (m_inh_bank != 0))
{
m_upperbank.select(0);
m_inh_bank = 0;
}
}
}
/***************************************************************************
START/RESET
***************************************************************************/
void apple2_state::machine_start()
{
m_ram_ptr = m_ram->pointer();
m_ram_size = m_ram->size();
m_speaker_state = 0;
m_speaker->level_w(m_speaker_state);
m_cassette_state = 0;
m_cassette->output(-1.0f);
m_upperbank.select(0);
m_inh_bank = 0;
m_strobe = 0;
m_transchar = 0;
// precalculate joystick time constants
m_x_calibration = attotime::from_nsec(10800).as_double();
m_y_calibration = attotime::from_nsec(10800).as_double();
// cache slot devices
for (int i = 0; i <= 7; i++)
{
m_slotdevice[i] = m_a2bus->get_a2bus_card(i);
}
for (int adr = 0; adr < m_ram_size; adr += 2)
{
m_ram_ptr[adr] = 0;
m_ram_ptr[adr+1] = 0xff;
}
m_joystick_x1_time = m_joystick_x2_time = m_joystick_y1_time = m_joystick_y2_time = 0;
m_reset_latch = false;
// setup save states
save_item(NAME(m_speaker_state));
save_item(NAME(m_cassette_state));
save_item(NAME(m_joystick_x1_time));
save_item(NAME(m_joystick_y1_time));
save_item(NAME(m_joystick_x2_time));
save_item(NAME(m_joystick_y2_time));
save_item(NAME(m_lastchar));
save_item(NAME(m_strobe));
save_item(NAME(m_transchar));
save_item(NAME(m_inh_slot));
save_item(NAME(m_inh_bank));
save_item(NAME(m_cnxx_slot));
save_item(NAME(m_anykeydown));
save_item(NAME(m_reset_latch));
// setup video pointers
m_video->set_ram_pointers(m_ram_ptr, m_ram_ptr);
m_video->set_char_pointer(memregion("gfx1")->base(), memregion("gfx1")->bytes());
}
void apple2_state::machine_reset()
{
m_inh_slot = 0;
m_cnxx_slot = -1;
m_anykeydown = false;
// reset the cards
m_a2bus->reset_bus();
// reset the 6502 now as a card may have pulled /INH on the reset vector
logerror("machine_reset\n");
m_maincpu->set_input_line(INPUT_LINE_RESET, ASSERT_LINE);
m_maincpu->set_input_line(INPUT_LINE_RESET, CLEAR_LINE);
}
/***************************************************************************
VIDEO
***************************************************************************/
TIMER_DEVICE_CALLBACK_MEMBER(apple2_state::apple2_interrupt)
{
int scanline = param;
if (scanline == 192)
{
// check reset
if ((m_resetdip.found()) && (m_resetdip->read() & 1)) // if reset DIP is present, use it
{
// CTRL-RESET
if ((m_kbspecial->read() & 0x88) == 0x88)
{
if (!m_reset_latch)
{
m_reset_latch = true;
m_maincpu->set_input_line(INPUT_LINE_RESET, ASSERT_LINE);
}
}
else
{
if (m_reset_latch)
{
m_reset_latch = false;
// allow cards to see reset
m_a2bus->reset_bus();
m_maincpu->set_input_line(INPUT_LINE_RESET, CLEAR_LINE);
}
}
}
else // plain RESET
{
if (m_kbspecial->read() & 0x80)
{
if (!m_reset_latch)
{
m_reset_latch = true;
m_maincpu->set_input_line(INPUT_LINE_RESET, ASSERT_LINE);
}
}
else
{
if (m_reset_latch)
{
m_reset_latch = false;
// allow cards to see reset
m_a2bus->reset_bus();
m_maincpu->set_input_line(INPUT_LINE_RESET, CLEAR_LINE);
}
}
}
}
}
/***************************************************************************
I/O
***************************************************************************/
u8 apple2_state::keyb_data_r()
{
// keyboard latch
return m_transchar | m_strobe;
}
u8 apple2_state::keyb_strobe_r()
{
// reads floating bus, clears strobe
if (!machine().side_effects_disabled())
{
m_strobe = 0;
}
return read_floatingbus();
}
void apple2_state::keyb_strobe_w(u8 data)
{
// clear keyboard latch
m_strobe = 0;
}
u8 apple2_state::cassette_toggle_r()
{
if (!machine().side_effects_disabled())
cassette_toggle_w(0);
return read_floatingbus();
}
void apple2_state::cassette_toggle_w(u8 data)
{
m_cassette_state ^= 1;
m_cassette->output(m_cassette_state ? 1.0f : -1.0f);
}
u8 apple2_state::speaker_toggle_r()
{
if (!machine().side_effects_disabled())
speaker_toggle_w(0);
return read_floatingbus();
}
void apple2_state::speaker_toggle_w(u8 data)
{
m_speaker_state ^= 1;
m_speaker->level_w(m_speaker_state);
}
u8 apple2_state::utility_strobe_r()
{
if (!machine().side_effects_disabled())
utility_strobe_w(0);
return read_floatingbus();
}
void apple2_state::utility_strobe_w(u8 data)
{
// low pulse on pin 5 of game I/O connector
m_gameio->strobe_w(0);
m_gameio->strobe_w(1);
}
u8 apple2_state::switches_r(offs_t offset)
{
if (!machine().side_effects_disabled())
m_softlatch->write_bit((offset & 0x0e) >> 1, offset & 0x01);
return read_floatingbus();
}
u8 apple2_state::flags_r(offs_t offset)
{
u8 uFloatingBus7 = read_floatingbus() & 0x7f;
// Y output of 74LS251 at H14 read as D7
switch (offset)
{
case 0: // cassette in (accidentally read at $C068 by ProDOS to attempt IIgs STATE register)
return (m_cassette->input() > 0.0 ? 0x80 : 0) | uFloatingBus7;
case 1: // button 0
return (m_gameio->sw0_r() ? 0x80 : 0) | uFloatingBus7;
case 2: // button 1
return (m_gameio->sw1_r() ? 0x80 : 0) | uFloatingBus7;
case 3: // button 2
// check if SHIFT key mod configured
if (m_sysconfig->read() & 0x04)
{
return ((m_gameio->sw2_r() || (m_kbspecial->read() & 0x06)) ? 0x80 : 0) | uFloatingBus7;
}
return (m_gameio->sw2_r() ? 0x80 : 0) | (read_floatingbus() & 0x7f);
case 4: // joy 1 X axis
if (!m_gameio->is_device_connected()) return 0x80 | uFloatingBus7;
return ((machine().time().as_double() < m_joystick_x1_time) ? 0x80 : 0) | uFloatingBus7;
case 5: // joy 1 Y axis
if (!m_gameio->is_device_connected()) return 0x80 | uFloatingBus7;
return ((machine().time().as_double() < m_joystick_y1_time) ? 0x80 : 0) | uFloatingBus7;
case 6: // joy 2 X axis
if (!m_gameio->is_device_connected()) return 0x80 | uFloatingBus7;
return ((machine().time().as_double() < m_joystick_x2_time) ? 0x80 : 0) | uFloatingBus7;
case 7: // joy 2 Y axis
if (!m_gameio->is_device_connected()) return 0x80 | uFloatingBus7;
return ((machine().time().as_double() < m_joystick_y2_time) ? 0x80 : 0) | uFloatingBus7;
}
// this is never reached
return 0;
}
u8 apple2_state::controller_strobe_r()
{
if (!machine().side_effects_disabled())
controller_strobe_w(0);
return read_floatingbus();
}
void apple2_state::controller_strobe_w(u8 data)
{
// 558 monostable one-shot timers; a running timer cannot be restarted
if (machine().time().as_double() >= m_joystick_x1_time)
{
m_joystick_x1_time = machine().time().as_double() + m_x_calibration * m_gameio->pdl0_r();
}
if (machine().time().as_double() >= m_joystick_y1_time)
{
m_joystick_y1_time = machine().time().as_double() + m_y_calibration * m_gameio->pdl1_r();
}
if (machine().time().as_double() >= m_joystick_x2_time)
{
m_joystick_x2_time = machine().time().as_double() + m_x_calibration * m_gameio->pdl2_r();
}
if (machine().time().as_double() >= m_joystick_y2_time)
{
m_joystick_y2_time = machine().time().as_double() + m_y_calibration * m_gameio->pdl3_r();
}
}
u8 apple2_state::c080_r(offs_t offset)
{
if(!machine().side_effects_disabled())
{
int slot;
offset &= 0x7F;
slot = offset / 0x10;
if (m_slotdevice[slot] != nullptr)
{
return m_slotdevice[slot]->read_c0nx(offset % 0x10);
}
}
return read_floatingbus();
}
void apple2_state::c080_w(offs_t offset, u8 data)
{
int slot;
offset &= 0x7F;
slot = offset / 0x10;
if (m_slotdevice[slot] != nullptr)
{
m_slotdevice[slot]->write_c0nx(offset % 0x10, data);
}
}
u8 apple2_state::c100_r(offs_t offset)
{
int slotnum;
slotnum = ((offset>>8) & 0xf) + 1;
if (m_slotdevice[slotnum] != nullptr)
{
if ((m_slotdevice[slotnum]->take_c800()) && (!machine().side_effects_disabled()))
{
m_cnxx_slot = slotnum;
}
return m_slotdevice[slotnum]->read_cnxx(offset&0xff);
}
return read_floatingbus();
}
void apple2_state::c100_w(offs_t offset, u8 data)
{
int slotnum;
slotnum = ((offset>>8) & 0xf) + 1;
if (m_slotdevice[slotnum] != nullptr)
{
if ((m_slotdevice[slotnum]->take_c800()) && (!machine().side_effects_disabled()))
{
m_cnxx_slot = slotnum;
}
m_slotdevice[slotnum]->write_cnxx(offset&0xff, data);
}
}
u8 apple2_state::c800_r(offs_t offset)
{
if (offset == 0x7ff)
{
u8 rv = 0xff;
if ((m_cnxx_slot != -1) && (m_slotdevice[m_cnxx_slot] != nullptr))
{
rv = m_slotdevice[m_cnxx_slot]->read_c800(offset&0xfff);
}
if (!machine().side_effects_disabled())
{
m_cnxx_slot = -1;
}
return rv;
}
if ((m_cnxx_slot != -1) && (m_slotdevice[m_cnxx_slot] != nullptr))
{
return m_slotdevice[m_cnxx_slot]->read_c800(offset&0xfff);
}
return read_floatingbus();
}
void apple2_state::c800_w(offs_t offset, u8 data)
{
if ((m_cnxx_slot != -1) && (m_slotdevice[m_cnxx_slot] != nullptr))
{
m_slotdevice[m_cnxx_slot]->write_c800(offset&0xfff, data);
}
if (offset == 0x7ff)
{
if (!machine().side_effects_disabled())
{
m_cnxx_slot = -1;
}
return;
}
}
u8 apple2_state::inh_r(offs_t offset)
{
if (m_inh_slot != -1)
{
return m_slotdevice[m_inh_slot]->read_inh_rom(offset + 0xd000);
}
assert(0); // hitting inh_r with invalid m_inh_slot should not be possible
return read_floatingbus();
}
void apple2_state::inh_w(offs_t offset, u8 data)
{
if ((m_inh_slot != -1) && (m_slotdevice[m_inh_slot] != nullptr))
{
m_slotdevice[m_inh_slot]->write_inh_rom(offset + 0xd000, data);
}
}
// floating bus code from old machine/apple2: now works reasonably well with French Touch and Deater "vapor lock" stuff
u8 apple2_state::read_floatingbus()
{
enum
{
// scanner constants
kHBurstClock = 53, // clock when Color Burst starts
kHBurstClocks = 4, // clocks per Color Burst duration
kHClock0State = 0x18, // H[543210] = 011000
kHClocks = 65, // clocks per horizontal scan (including HBL)
kHPEClock = 40, // clock when HPE (horizontal preset enable) goes low
kHPresetClock = 41, // clock when H state presets
kHSyncClock = 49, // clock when HSync starts
kHSyncClocks = 4, // clocks per HSync duration
kNTSCScanLines = 262, // total scan lines including VBL (NTSC)
kNTSCVSyncLine = 224, // line when VSync starts (NTSC)
kPALScanLines = 312, // total scan lines including VBL (PAL)
kPALVSyncLine = 264, // line when VSync starts (PAL)
kVLine0State = 0x100, // V[543210CBA] = 100000000
kVPresetLine = 256, // line when V state presets
kVSyncLines = 4, // lines per VSync duration
kClocksPerVSync = kHClocks * kNTSCScanLines // FIX: NTSC only?
};
// vars
//
int i, Hires, Mixed, Page2, _80Store, ScanLines, /* VSyncLine, ScanCycles,*/
h_clock, h_state, h_0, h_1, h_2, h_3, h_4, h_5,
v_line, v_state, v_A, v_B, v_C, v_0, v_1, v_2, v_3, v_4, /* v_5, */
_hires, addend0, addend1, addend2, sum, address;
// video scanner data
//
i = m_maincpu->total_cycles() % kClocksPerVSync; // cycles into this VSync
// machine state switches
//
Hires = (m_video->get_hires() && m_video->get_graphics()) ? 1 : 0;
Mixed = m_video->get_mix() ? 1 : 0;
Page2 = m_video->get_page2() ? 1 : 0;
_80Store = 0;
// calculate video parameters according to display standard
//
ScanLines = 1 ? kNTSCScanLines : kPALScanLines; // FIX: NTSC only?
// VSyncLine = 1 ? kNTSCVSyncLine : kPALVSyncLine; // FIX: NTSC only?
// ScanCycles = ScanLines * kHClocks;
// calculate horizontal scanning state
h_clock = (i + 63) % kHClocks; // which horizontal scanning clock
h_state = kHClock0State + h_clock; // H state bits
if (h_clock >= kHPresetClock) // check for horizontal preset
{
h_state -= 1; // correct for state preset (two 0 states)
}
h_0 = (h_state >> 0) & 1; // get horizontal state bits
h_1 = (h_state >> 1) & 1;
h_2 = (h_state >> 2) & 1;
h_3 = (h_state >> 3) & 1;
h_4 = (h_state >> 4) & 1;
h_5 = (h_state >> 5) & 1;
// calculate vertical scanning state
//
v_line = (i / kHClocks) + 192; // which vertical scanning line
v_state = kVLine0State + v_line; // V state bits
if ((v_line >= kVPresetLine)) // check for previous vertical state preset
{
v_state -= ScanLines; // compensate for preset
}
v_A = (v_state >> 0) & 1; // get vertical state bits
v_B = (v_state >> 1) & 1;
v_C = (v_state >> 2) & 1;
v_0 = (v_state >> 3) & 1;
v_1 = (v_state >> 4) & 1;
v_2 = (v_state >> 5) & 1;
v_3 = (v_state >> 6) & 1;
v_4 = (v_state >> 7) & 1;
//v_5 = (v_state >> 8) & 1;
// calculate scanning memory address
//
_hires = Hires;
if (Hires && Mixed && (v_4 & v_2))
{
_hires = 0; // (address is in text memory)
}
addend0 = 0x68; // 1 1 0 1
addend1 = (h_5 << 5) | (h_4 << 4) | (h_3 << 3);
addend2 = (v_4 << 6) | (v_3 << 5) | (v_4 << 4) | (v_3 << 3);
sum = (addend0 + addend1 + addend2) & (0x0F << 3);
address = 0;
address |= h_0 << 0; // a0
address |= h_1 << 1; // a1
address |= h_2 << 2; // a2
address |= sum; // a3 - aa6
address |= v_0 << 7; // a7
address |= v_1 << 8; // a8
address |= v_2 << 9; // a9
address |= ((_hires) ? v_A : (1 ^ (Page2 & (1 ^ _80Store)))) << 10; // a10
address |= ((_hires) ? v_B : (Page2 & (1 ^ _80Store))) << 11; // a11
if (_hires) // hires?
{
// Y: insert hires only address bits
//
address |= v_C << 12; // a12
address |= (1 ^ (Page2 & (1 ^ _80Store))) << 13; // a13
address |= (Page2 & (1 ^ _80Store)) << 14; // a14
}
else
{
// N: text, so no higher address bits unless Apple ][, not Apple //e
//
if ((kHPEClock <= h_clock) && // Y: HBL?
(h_clock <= (kHClocks - 1)))
{
address |= 1 << 12; // Y: a12 (add $1000 to address!)
}
}
return m_ram_ptr[address % m_ram_size];
}
/***************************************************************************
ADDRESS MAP
***************************************************************************/
u8 apple2_state::ram_r(offs_t offset)
{
if (offset < m_ram_size)
{
return m_ram_ptr[offset];
}
return 0xff;
}
void apple2_state::ram_w(offs_t offset, u8 data)
{
if (offset < m_ram_size)
{
m_ram_ptr[offset] = data;
}
}
void apple2_state::apple2_map(address_map &map)
{
map(0x0000, 0xbfff).rw(FUNC(apple2_state::ram_r), FUNC(apple2_state::ram_w));
map(0xc000, 0xc000).mirror(0xf).r(FUNC(apple2_state::keyb_data_r)).nopw();
map(0xc010, 0xc010).mirror(0xf).rw(FUNC(apple2_state::keyb_strobe_r), FUNC(apple2_state::keyb_strobe_w));
map(0xc020, 0xc020).mirror(0xf).rw(FUNC(apple2_state::cassette_toggle_r), FUNC(apple2_state::cassette_toggle_w));
map(0xc030, 0xc030).mirror(0xf).rw(FUNC(apple2_state::speaker_toggle_r), FUNC(apple2_state::speaker_toggle_w));
map(0xc040, 0xc040).mirror(0xf).rw(FUNC(apple2_state::utility_strobe_r), FUNC(apple2_state::utility_strobe_w));
map(0xc050, 0xc05f).r(FUNC(apple2_state::switches_r)).w(m_softlatch, FUNC(addressable_latch_device::write_a0));
map(0xc060, 0xc067).mirror(0x8).r(FUNC(apple2_state::flags_r)).nopw(); // includes IIgs STATE register, which ProDOS touches
map(0xc070, 0xc070).mirror(0xf).rw(FUNC(apple2_state::controller_strobe_r), FUNC(apple2_state::controller_strobe_w));
map(0xc080, 0xc0ff).rw(FUNC(apple2_state::c080_r), FUNC(apple2_state::c080_w));
map(0xc100, 0xc7ff).rw(FUNC(apple2_state::c100_r), FUNC(apple2_state::c100_w));
map(0xc800, 0xcfff).rw(FUNC(apple2_state::c800_r), FUNC(apple2_state::c800_w));
map(0xd000, 0xffff).view(m_upperbank);
m_upperbank[0](0xd000, 0xffff).rom().region("maincpu", 0x1000).w(FUNC(apple2_state::inh_w));
m_upperbank[1](0xd000, 0xffff).rw(FUNC(apple2_state::inh_r), FUNC(apple2_state::inh_w));
}
/***************************************************************************
KEYBOARD
***************************************************************************/
int apple2_state::ay3600_shift_r()
{
// either shift key
if (m_kbspecial->read() & 0x06)
{
return ASSERT_LINE;
}
return CLEAR_LINE;
}
int apple2_state::ay3600_control_r()
{
if (m_kbspecial->read() & 0x08)
{
return ASSERT_LINE;
}
return CLEAR_LINE;
}
static const u8 a2_key_remap[0x32][4] =
{
/* norm shft ctrl both */
{ 0x33,0x23,0x33,0x23 }, /* 3 # 00 */
{ 0x34,0x24,0x34,0x24 }, /* 4 $ 01 */
{ 0x35,0x25,0x35,0x25 }, /* 5 % 02 */
{ 0x36,'&', 0x35,'&' }, /* 6 & 03 */
{ 0x37,0x27,0x37,0x27 }, /* 7 ' 04 */
{ 0x38,0x28,0x38,0x28 }, /* 8 ( 05 */
{ 0x39,0x29,0x39,0x29 }, /* 9 ) 06 */
{ 0x30,0x30,0x30,0x30 }, /* 0 07 */
{ 0x3a,0x2a,0x3a,0x2a }, /* : * 08 */
{ 0x2d,0x3d,0x2d,0x3d }, /* - = 09 */
{ 0x51,0x51,0x11,0x11 }, /* q Q 0a */
{ 0x57,0x57,0x17,0x17 }, /* w W 0b */
{ 0x45,0x45,0x05,0x05 }, /* e E 0c */
{ 0x52,0x52,0x12,0x12 }, /* r R 0d */
{ 0x54,0x54,0x14,0x14 }, /* t T 0e */
{ 0x59,0x59,0x19,0x19 }, /* y Y 0f */
{ 0x55,0x55,0x15,0x15 }, /* u U 10 */
{ 0x49,0x49,0x09,0x09 }, /* i I 11 */
{ 0x4f,0x4f,0x0f,0x0f }, /* o O 12 */
{ 0x50,0x40,0x10,0x00 }, /* p P 13 */
{ 0x44,0x44,0x04,0x04 }, /* d D 14 */
{ 0x46,0x46,0x06,0x06 }, /* f F 15 */
{ 0x47,0x47,0x07,0x07 }, /* g G 16 */
{ 0x48,0x48,0x08,0x08 }, /* h H 17 */
{ 0x4a,0x4a,0x0a,0x0a }, /* j J 18 */
{ 0x4b,0x4b,0x0b,0x0b }, /* k K 19 */
{ 0x4c,0x4c,0x0c,0x0c }, /* l L 1a */
{ ';' ,0x2b,';' ,0x2b }, /* ; + 1b */
{ 0x08,0x08,0x08,0x08 }, /* Left 1c */
{ 0x15,0x15,0x15,0x15 }, /* Right 1d */
{ 0x5a,0x5a,0x1a,0x1a }, /* z Z 1e */
{ 0x58,0x58,0x18,0x18 }, /* x X 1f */
{ 0x43,0x43,0x03,0x03 }, /* c C 20 */
{ 0x56,0x56,0x16,0x16 }, /* v V 21 */
{ 0x42,0x42,0x02,0x02 }, /* b B 22 */
{ 0x4e,0x5e,0x0e,0x1e }, /* n N 23 */
{ 0x4d,0x5d,0x0d,0x1d }, /* m M 24 */
{ 0x2c,0x3c,0x2c,0x3c }, /* , < 25 */
{ 0x2e,0x3e,0x2e,0x3e }, /* . > 26 */
{ 0x2f,0x3f,0x2f,0x3f }, /* / ? 27 */
{ 0x53,0x53,0x13,0x13 }, /* s S 28 */
{ 0x32,0x22,0x32,0x00 }, /* 2 " 29 */
{ 0x31,0x21,0x31,0x31 }, /* 1 ! 2a */
{ 0x1b,0x1b,0x1b,0x1b }, /* Escape 2b */
{ 0x41,0x41,0x01,0x01 }, /* a A 2c */
{ 0x20,0x20,0x20,0x20 }, /* Space 2d */
{ 0x00,0x00,0x20,0x00 }, /* 0x2e unused */
{ 0x00,0x00,0x20,0x00 }, /* 0x2f unused */
{ 0x00,0x00,0x20,0x00 }, /* 0x30 unused */
{ 0x0d,0x0d,0x0d,0x0d }, /* Enter 31 */
};
void apple2_state::ay3600_data_ready_w(int state)
{
if (state == ASSERT_LINE)
{
int mod = 0;
// if the user presses a valid key to start the driver from the info screen,
// we will see that key. ignore keys in the first 25,000 cycles (in my tests,
// the unwanted key shows up at 17030 cycles)
if (m_maincpu->total_cycles() < 25000)
{
return;
}
m_lastchar = m_ay3600->b_r();
mod = (m_kbspecial->read() & 0x06) ? 0x01 : 0x00;
mod |= (m_kbspecial->read() & 0x08) ? 0x02 : 0x00;
m_transchar = a2_key_remap[m_lastchar&0x3f][mod];
m_strobe = 0x80;
// printf("new char = %04x (%02x)\n", m_lastchar&0x3f, m_transchar);
}
}
void apple2_state::ay3600_ako_w(int state)
{
m_anykeydown = (state == ASSERT_LINE) ? true : false;
}
TIMER_DEVICE_CALLBACK_MEMBER(apple2_state::ay3600_repeat)
{
// is the key still down?
if (m_anykeydown)
{
if (m_kbrepeat->read() & 1)
{
m_strobe = 0x80;
}
}
}
/***************************************************************************
INPUT PORTS
***************************************************************************/
INPUT_PORTS_START( apple2_sysconfig )
PORT_START("a2_config")
PORT_CONFNAME(0x04, 0x04, "Shift key mod") // default to installed
PORT_CONFSETTING(0x00, "Not present")
PORT_CONFSETTING(0x04, "Installed")
INPUT_PORTS_END
/*
Apple II / II Plus key matrix (from "The Apple II Circuit Description")
| Y0 | Y1 | Y2 | Y3 | Y4 | Y5 | Y6 | Y7 | Y8 | Y9 |
| | | | | | | | | | |
----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----|
X0 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | :* | - |
----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----|
X1 | Q | W | E | R | T | Y | U | I | O | P |
----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----|
X2 | D | F | G | H | J | K | L | ;+ |LEFT |RIGHT|
----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----|
X3 | Z | X | C | V | B | N | M | ,< | .> | /? |
----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----|
X4 | S | 2 | 1 | ESC | A |SPACE| | | |ENTER|
----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----|
*/
static INPUT_PORTS_START( apple2_common )
PORT_START("X0")
PORT_BIT(0x001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_3) PORT_CHAR('3') PORT_CHAR('#')
PORT_BIT(0x002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_4) PORT_CHAR('4') PORT_CHAR('$')
PORT_BIT(0x004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_5) PORT_CHAR('5') PORT_CHAR('%')
PORT_BIT(0x008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_6) PORT_CHAR('6') PORT_CHAR('&')
PORT_BIT(0x010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_7) PORT_CHAR('7') PORT_CHAR('\'')
PORT_BIT(0x020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_8) PORT_CHAR('8') PORT_CHAR('(')
PORT_BIT(0x040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_9) PORT_CHAR('9') PORT_CHAR(')')
PORT_BIT(0x080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_0) PORT_CHAR('0')
PORT_BIT(0x100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_MINUS) PORT_CHAR(':') PORT_CHAR('*')
PORT_BIT(0x200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_EQUALS) PORT_CHAR('-') PORT_CHAR('=')
PORT_START("X1")
PORT_BIT(0x001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_CODE(KEYCODE_Q) PORT_CHAR('Q') PORT_CHAR('q')