/
mac128.cpp
1530 lines (1282 loc) · 53.7 KB
/
mac128.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, O. Galibert
/****************************************************************************
drivers/mac128.cpp
Original-style Macintosh emulation
These are all 68000 machines in the original Mac form factor with the
original Mac audio and video.
Unitron Mac 512: Brazilian Mac 512K(E?) clone.
Unitron 1024: Brazilian Mac Plus clone.
Driver by R. Belmont and O. Galibert, with thanks to the original Mac
driver authors Nathan Woods and Raphael Nabet.
Thanks also to SCSI guru Patrick Mackinlay and keyboard/mouse wrangler
Vas Crabb.
Mac 128K/512K: the original machines with 128K or 512K of RAM.
Mac Plus: floppy now double-sided 800K, SIMM slots for memory expansion,
SCSI interface added.
Mac 512KE: a Mac 512K with the 800K floppy drive and the newer Mac Plus ROMs.
Mac SE: Mac Plus with ADB for the keyboard and mouse interface, and an
expansion slot.
Mac SE FDHD: Mac SE with the IWM and 800K drive upgraded to SWIM and
the 1.44MB "SuperDrive".
Mac Classic: Cost-reduced Mac SE FDHD.
Memory Map:
0x000000 - 0x3fffff RAM/ROM (switches based on overlay)
0x400000 - 0x4fffff ROM
0x580000 - 0x5fffff 5380 NCR/Symbios SCSI peripherals chip (Mac Plus only)
0x600000 - 0x6fffff RAM
0x800000 - 0x9fffff Zilog 8530 SCC (Serial Communications Controller) Read
0xa00000 - 0xbfffff Zilog 8530 SCC (Serial Communications Controller) Write
0xc00000 - 0xdfffff IWM (Integrated Woz Machine) floppy controller
0xe80000 - 0xefffff Rockwell 6522 VIA
0xf00000 - 0xffffef Open bus??? (the ROM appears to be accessing here)
0xfffff0 - 0xffffff Auto Vector
Interrupts:
M68K:
Level 1 from VIA
Level 2 from SCC
Level 4 from "programmer switch" (not implemented)
VIA:
CA1 from VBLANK
CA2 from 1 Hz clock (RTC)
CB1 from Keyboard Clock
CB2 from Keyboard Data
SR from Keyboard Data Ready
SCC:
PB_EXT (DCDB) from mouse Y circuitry
PA_EXT (DCDA) from mouse X circuitry
The MC68000's FC outputs are not used even for autovectoring. The
VIA's address range is overdecoded to generate VPA for both.
VIA notes:
Original 128K Macs use Synertek SYP6522 VIAs. Synertek's AN5 (March 1982)
claims that their VIAs have a difference in shift register behavior
compared to those from other manufacturers. However, this particular
difference is not relevant to the Mac hardware, and later 512K Macs
switched away from SYP6522 (Apple part number 338-6522-A) to either
Rockwell R6522AP (R6522-66) or VTI VL6522-02PC. R6522AP is the most
common VIA on the Mac Plus.
Apple continued sourcing VIAs from the same two manufacturers for the
Mac SE, though their ADB-era VIAs are newer CMOS models with a fix for the
shift register's CB1 input synchronizer which Apple may have specifically
requested. The new Rockwell version (R65NC22) is only labeled with Apple
part number 338-6523 (later Macs use a PLCC version which Apple numbered
338S6523), but VLSI Technology's VL65C22V-02PC is not so disguised.
Raster timings from the BBU ERS:
There are 512 visible pixels (32.68 microseconds) per scanline plus 192 pixels
(12.25 microseconds) of hblank. Sound/PWM are fetched at the end of hblank.
Vertically there are 28 lines of vblank followed by 342 displayed lines.
Scanline 0 is the start of vblank.
****************************************************************************/
#include "emu.h"
#include "adbmodem.h"
#include "macrtc.h"
#include "mactoolbox.h"
#include "bus/mackbd/mackbd.h"
#include "bus/macpds/hyperdrive.h"
#include "bus/nscsi/cd.h"
#include "bus/nscsi/devices.h"
#include "cpu/m68000/m68000.h"
#include "machine/6522via.h"
#include "machine/iwm.h"
#include "machine/swim1.h"
#include "machine/ncr5380.h"
#include "machine/nscsi_bus.h"
#include "machine/rescap.h"
#include "bus/nscsi/devices.h"
#include "machine/ram.h"
#include "machine/applefdintf.h"
#include "machine/timer.h"
#include "machine/z80scc.h"
#include "macadb.h"
#include "macscsi.h"
#include "sound/dac.h"
#include "sound/flt_biquad.h"
#include "bus/macpds/pds_tpdfpd.h"
#include "formats/ap_dsk35.h"
#include "emupal.h"
#include "screen.h"
#include "softlist.h"
#include "speaker.h"
namespace {
#define C7M (15.6672_MHz_XTAL / 2)
#define C3_7M (15.6672_MHz_XTAL / 4).value()
// video parameters
static constexpr int MAC_H_VIS = 512;
static constexpr int MAC_V_VIS = 342;
static constexpr int MAC_H_TOTAL = 704; // (512+192)
static constexpr int MAC_V_TOTAL = 370; // (342+28)
// sound buffer locations
static constexpr int MAC_MAIN_SND_BUF_OFFSET = (0x0300>>1); // (end of memory minus 0x0300; for the typical macplus case, this is 0x3ffd00-0x3fffe3 in 16 bit blocks)
static constexpr int MAC_ALT_SND_BUF_OFFSET = (0x5F00>>1); // (end of memory minus 0x5F00)
class mac128_state : public driver_device
{
public:
mac128_state(const machine_config &mconfig, device_type type, const char *tag) :
driver_device(mconfig, type, tag),
m_maincpu(*this, "maincpu"),
m_via(*this, "via6522_0"),
m_adbmodem(*this, "adbmodem"),
m_macadb(*this, "macadb"),
m_ram(*this, RAM_TAG),
m_scsibus(*this, "scsibus"),
m_scsihelp(*this, "scsihelp"),
m_ncr5380(*this, "scsibus:7:ncr5380"),
m_iwm(*this, "fdc"),
m_floppy(*this, "fdc:%d", 0U),
m_mackbd(*this, "kbd"),
m_rtc(*this, "rtc"),
m_screen(*this, "screen"),
m_dac(*this, "macdac"),
m_filter(*this, "dacfilter"),
m_volfilter(*this, "volfilter"),
m_scc(*this, "scc"),
m_mouse0(*this, "MOUSE0"),
m_mouse1(*this, "MOUSE1"),
m_mouse2(*this, "MOUSE2"),
m_cur_floppy(nullptr),
m_hdsel(0),
m_devsel(0)
{
}
void mac512ke(machine_config &config);
void mac128k(machine_config &config);
void mac512k(machine_config &config);
void macplus(machine_config &config);
void macse(machine_config &config);
void macsefd(machine_config &config);
void macclasc(machine_config &config);
void mac_driver_init();
private:
required_device<m68000_device> m_maincpu;
required_device<via6522_device> m_via;
optional_device<adbmodem_device> m_adbmodem;
optional_device<macadb_device> m_macadb;
required_device<ram_device> m_ram;
optional_device<nscsi_bus_device> m_scsibus;
optional_device<mac_scsi_helper_device> m_scsihelp;
optional_device<ncr5380_device> m_ncr5380;
required_device<applefdintf_device> m_iwm;
required_device_array<floppy_connector, 2> m_floppy;
optional_device<mac_keyboard_port_device> m_mackbd;
required_device<rtc3430042_device> m_rtc;
required_device<screen_device> m_screen;
required_device<dac_12bit_r2r_device> m_dac; // actually 1-bit pwm w/8-bit counters
required_device<filter_biquad_device> m_filter;
required_device<filter_biquad_device> m_volfilter;
required_device<z80scc_device> m_scc;
optional_ioport m_mouse0, m_mouse1, m_mouse2;
virtual void machine_start() override;
virtual void machine_reset() override;
void scc_mouse_irq( int x, int y );
void set_via_interrupt(int value);
void field_interrupts();
void vblank_irq();
void mouse_callback();
uint16_t ram_r(offs_t offset);
void ram_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
void ram_w_se(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
uint16_t ram_600000_r(offs_t offset);
void ram_600000_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~ 0);
uint16_t mac_via_r(offs_t offset);
void mac_via_w(offs_t offset, uint16_t data);
uint16_t mac_autovector_r(offs_t offset);
void mac_autovector_w(offs_t offset, uint16_t data);
uint16_t mac_iwm_r(offs_t offset, uint16_t mem_mask = ~0);
void mac_iwm_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
uint16_t macplus_scsi_r(offs_t offset, uint16_t mem_mask = ~0);
void macplus_scsi_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
uint16_t macse_scsi_r(offs_t offset, uint16_t mem_mask = ~0);
void macse_scsi_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
void scsi_irq_w(int state);
void scsi_drq_w(int state);
void scsi_berr_w(uint8_t data);
void set_scc_interrupt(int state);
void vblank_w(int state);
void adb_irq_w(int state) { m_adb_irq_pending = state; }
TIMER_CALLBACK_MEMBER(mac_scanline);
TIMER_CALLBACK_MEMBER(mac_hblank);
uint32_t screen_update_mac(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect);
uint8_t mac_via_in_a();
uint8_t mac_via_in_b();
uint8_t mac_via_in_b_se();
void mac_via_out_a(uint8_t data);
void mac_via_out_b(uint8_t data);
void mac_via_out_a_se(uint8_t data);
void mac_via_out_b_se(uint8_t data);
void mac_via_irq(int state);
void update_volume();
void mac512ke_map(address_map &map);
void macplus_map(address_map &map);
void macse_map(address_map &map);
floppy_image_device *m_cur_floppy;
int m_hdsel, m_devsel;
int m_pwm_count_total = 0, m_pwm_count_1 = 0;
float m_pwm_current_rpm[2]{};
void phases_w(uint8_t phases);
void devsel_w(uint8_t devsel);
void devsel_se_w(uint8_t devsel);
void snd_push(uint8_t data);
void pwm_push(uint8_t data);
uint32_t m_overlay = 0;
uint8_t m_mouse_bit[2]{}, m_mouse_last[2]{};
int16_t m_mouse_last_m[2]{}, m_mouse_count[2]{};
int m_screen_buffer = 0;
emu_timer *m_scan_timer = nullptr;
emu_timer *m_hblank_timer = nullptr;
// interrupts
int m_scc_interrupt = 0, m_via_interrupt = 0, m_scsi_interrupt = 0, m_last_taken_interrupt = 0;
// DRQ
int m_scsi_drq = 0;
// wait states for accessing the VIA
bool m_snd_enable = false;
bool m_main_buffer = false;
int m_snd_vol = 0;
int m_adb_irq_pending = 0;
int m_drive_select = 0;
int m_scsiirq_enable = 0;
u16 *m_ram_ptr = nullptr, *m_rom_ptr = nullptr;
u32 m_ram_mask = 0, m_ram_size = 0;
};
void mac128_state::machine_start()
{
m_ram_ptr = (u16*)m_ram->pointer();
m_ram_size = m_ram->size()>>1;
m_ram_mask = m_ram_size - 1;
m_rom_ptr = (u16*)memregion("bootrom")->base();
m_scan_timer = timer_alloc(FUNC(mac128_state::mac_scanline), this);
m_hblank_timer = timer_alloc(FUNC(mac128_state::mac_hblank), this);
save_item(NAME(m_overlay));
save_item(NAME(m_mouse_bit));
save_item(NAME(m_mouse_last));
save_item(NAME(m_mouse_last_m));
save_item(NAME(m_mouse_count));
save_item(NAME(m_screen_buffer));
save_item(NAME(m_scc_interrupt));
save_item(NAME(m_via_interrupt));
save_item(NAME(m_scsi_interrupt));
save_item(NAME(m_scsi_drq));
save_item(NAME(m_last_taken_interrupt));
save_item(NAME(m_snd_enable));
save_item(NAME(m_main_buffer));
save_item(NAME(m_snd_vol));
save_item(NAME(m_adb_irq_pending));
save_item(NAME(m_drive_select));
save_item(NAME(m_scsiirq_enable));
save_item(NAME(m_pwm_count_total));
save_item(NAME(m_pwm_count_1));
save_item(NAME(m_pwm_current_rpm));
m_mouse_bit[0] = m_mouse_bit[1] = 0;
m_mouse_last[0] = m_mouse_last[1] = 0;
}
void mac128_state::machine_reset()
{
m_last_taken_interrupt = -1;
m_overlay = 1;
m_screen_buffer = 1;
m_last_taken_interrupt = 0;
m_snd_enable = false;
m_main_buffer = true;
m_snd_vol = 3;
m_adb_irq_pending = 0;
m_drive_select = 0;
m_scsiirq_enable = 0;
m_pwm_count_total = 0;
m_pwm_count_1 = 0;
m_pwm_current_rpm[0] = 302.5; // Speed for 0% duty cycle
m_pwm_current_rpm[1] = 302.5;
m_scsi_drq = 0;
const int next_vpos = m_screen->vpos() + 1;
m_scan_timer->adjust(m_screen->time_until_pos(next_vpos), next_vpos);
if (m_screen->vblank())
m_via->write_pb6(0);
}
uint16_t mac128_state::ram_r(offs_t offset)
{
if (m_overlay)
{
return m_rom_ptr[offset & 0x7ffff];
}
return m_ram_ptr[offset & m_ram_mask];
}
void mac128_state::ram_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
if (!m_overlay)
{
COMBINE_DATA(&m_ram_ptr[offset & m_ram_mask]);
}
}
void mac128_state::ram_w_se(offs_t offset, uint16_t data, uint16_t mem_mask)
{
m_overlay = 0;
COMBINE_DATA(&m_ram_ptr[offset & m_ram_mask]);
}
uint16_t mac128_state::ram_600000_r(offs_t offset)
{
return m_ram_ptr[offset & m_ram_mask];
}
void mac128_state::ram_600000_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
COMBINE_DATA(&m_ram_ptr[offset & m_ram_mask]);
}
void mac128_state::field_interrupts()
{
int take_interrupt = -1;
if ((m_scc_interrupt) || (m_scsi_interrupt))
{
take_interrupt = 2;
}
else if (m_via_interrupt)
{
take_interrupt = 1;
}
// printf("field_interrupts: take %d\n", take_interrupt);
if (m_last_taken_interrupt > -1)
{
m_maincpu->set_input_line(m_last_taken_interrupt, CLEAR_LINE);
m_last_taken_interrupt = -1;
}
if (take_interrupt > -1)
{
m_maincpu->set_input_line(take_interrupt, ASSERT_LINE);
m_last_taken_interrupt = take_interrupt;
}
}
void mac128_state::set_scc_interrupt(int state)
{
// printf("SCC IRQ: %d\n", state);
m_scc_interrupt = state;
field_interrupts();
}
void mac128_state::set_via_interrupt(int value)
{
m_via_interrupt = value;
field_interrupts();
}
void mac128_state::vblank_irq()
{
if (m_macadb)
{
m_macadb->adb_vblank();
}
}
void mac128_state::update_volume()
{
/* LS161 audio PWM counters TC (SND) -> LS04 inverter (/SND) ->
* -> CD4016 gate A pulling a 5.1V zener-regulated signal to ground if input is high ->
* -> Sallen-key low-pass filter (R1 = 47K, R2 = 47K, C1 = 0.001uF, C2 = 470pF
* FC of 4939.3903Hz, Q of 0.7293, Gain of 1.0) ->
* ->\-> r13 (470k) ------------------------>|
* |-> r12 (470k) -> CD4016 D (pa0 != 0) ->|
* |-> r17 (150k) -> CD4016 C (pa1 != 0) ->|
* |-> r16 (68k) -> CD4016 B (pa2 != 0) ->\-> DC blocking caps ->
* -> Push-Pull +12v/-12vb amplifier w/feedback (technically a 1st order multifeedback lowpass filter?) ->
* -> Audio Jack -> Speaker
*/
const double res_ohm_tbl[8] =
{
// R13 R16 R17 R12
(1.0 / ( (1.0 / RES_K(470)) ) ),
(1.0 / ( (1.0 / RES_K(470)) + (1.0 / RES_K(470)) ) ),
(1.0 / ( (1.0 / RES_K(470)) + (1.0 / RES_K(150)) ) ),
(1.0 / ( (1.0 / RES_K(470)) + (1.0 / RES_K(150)) + (1.0 / RES_K(470)) ) ),
(1.0 / ( (1.0 / RES_K(470)) + (1.0 / RES_K(68)) ) ),
(1.0 / ( (1.0 / RES_K(470)) + (1.0 / RES_K(68)) + (1.0 / RES_K(470)) ) ),
(1.0 / ( (1.0 / RES_K(470)) + (1.0 / RES_K(68)) + (1.0 / RES_K(150)) ) ),
(1.0 / ( (1.0 / RES_K(470)) + (1.0 / RES_K(68)) + (1.0 / RES_K(150)) + (1.0 / RES_K(470)) ) )
};
m_volfilter->opamp_mfb_lowpass_modify(res_ohm_tbl[m_snd_vol&7], RES_K(0), RES_K(200), CAP_U(0), CAP_P(220)); // variable based on cd4016, short, R15, absent, C10
}
void mac128_state::vblank_w(int state)
{
m_via->write_ca1(state);
}
TIMER_CALLBACK_MEMBER(mac128_state::mac_scanline)
{
const int scanline = param;
if (scanline == 0)
{
vblank_irq();
}
/* video beam in display (! VBLANK && ! HBLANK basically) */
if (scanline >= 28)
{
m_via->write_pb6(1);
}
m_hblank_timer->adjust(m_screen->time_until_pos(scanline, MAC_H_TOTAL));
if ((!(scanline % 10)) && (!m_macadb))
{
mouse_callback();
}
m_scan_timer->adjust(m_screen->time_until_pos(scanline+1), (scanline+1) % m_screen->height());
}
TIMER_CALLBACK_MEMBER(mac128_state::mac_hblank)
{
const int scanline = m_screen->vpos();
uint16_t *mac_snd_buf_ptr;
if (m_main_buffer)
{
mac_snd_buf_ptr = (uint16_t *)(m_ram_ptr + m_ram_size - MAC_MAIN_SND_BUF_OFFSET);
}
else
{
mac_snd_buf_ptr = (uint16_t *)(m_ram_ptr + m_ram_size - MAC_ALT_SND_BUF_OFFSET);
}
// The sound "DAC" is a 1-bit PWM output driven by two 4-bit LS161 counters
// chained together. These counters are reset at the end of HBLANK, and
// count up once every C7M clock (2 pixels per clock), with the TC (SND)
// output connected (through two inverters) to the speaker filter. The
// counters count a max of 256 C7M clocks before reaching terminal count
// and halting themselves, and there are 704 / 2 = 352 C7M clocks per
// scanline, so this means the TC (SND) output over time is an asymmetric
// PWM squarewave, low from between 0 (if the load value is 0xff) and
// 255 (if the load value is 0x00) of the 352 C7M clocks per scanline,
// and high the remainder of the time. This has a significant DC offset
// due to the remaining clocks where the TC signal remains high.
// The counters can be forced to reset and be held at a value of 0x00 if
// the VIA PB7(/SNDRES) pin is held active(low), and this conversely will
// cause the TC (SND) counter pin to output a constant low level for as
// long as /SNDRES is held active.
// Some games such as Lode Runner use the sound manager "swMode" function,
// which uses the /SNDRES pin to alternately force the sound output low
// vs running normally in a square wave. During this time, the software is
// leaving the actual sound buffer FIFO values at a constant 0x80.
// So unless we force the 1-bit PWM to have a value of "always low" while
// PB7 is low, we get almost no sound in Lode Runner, and probably other
// games/software as well.
snd_push(mac_snd_buf_ptr[scanline] >> 8);
pwm_push(mac_snd_buf_ptr[scanline] & 0xff);
m_via->write_pb6(0);
}
void mac128_state::snd_push(uint8_t data)
{
double frac = (m_snd_enable ? (((double)(~data)) / (MAC_H_TOTAL / 2.0)) : 1.0);
m_dac->write((uint16_t)(frac * 4095.0)); // using a fraction of a 12 bit value, so we can handle inputs ranging between 0/352 and 352/352 without losing resolution.
}
void mac128_state::pwm_push(uint8_t data)
{
// The PWM works by sending pulses with a specific duty cycle.
// The lengths sent by the firmware are in the range 1-40, which
// means the total number of time slots is probably 42, to ensure
// at least one edge always happens. To get a better precision
// the firmware dithers between two values over a cycle of 10
// pulses, giving internally a 0-399 possible range mapping to a
// 11-410 real length out of 420 total, with a duty cycle ranging
// from 2.6% to 97.6%. The firmware calibrates from the drive
// actual rpm as measured through the tachometer with indexes 128
// and 256 at startup and keeps an eye on the actual rpm
// afterwards to avoid temperature drift.
// The length counter is a 6-bits lfsr with taps on bits 0 and 1
// and insertion on bit 5. The firmware writes a value so that
// the length is reached when the counter hits 0x20.
static const uint8_t value_to_length[64] = {
0, 1, 59, 2, 60, 40, 54, 3,
61, 32, 49, 41, 55, 19, 35, 4,
62, 52, 30, 33, 50, 12, 14, 42,
56, 16, 27, 20, 36, 23, 44, 5,
63, 58, 39, 53, 31, 48, 18, 34,
51, 29, 11, 13, 15, 26, 22, 43,
57, 38, 47, 17, 28, 10, 25, 21,
37, 46, 9, 24, 45, 8, 7, 6
};
m_pwm_count_1 += value_to_length[data & 0x3f];
m_pwm_count_total ++;
if (m_pwm_count_total == 100)
{
// The documentation requires:
// - duty cycle of 9.4%, 305 < rpm < 380 (middle 342.5)
// - duty cycle of 91%, 625 < rpm < 780 (middle 702.5)
// - linear between these two points
int internal_index = m_pwm_count_1 / (m_pwm_count_total/10) - 11;
if(internal_index < 0)
internal_index = 0;
if(internal_index > 399)
internal_index = 399;
float duty_cycle = internal_index / 419.0;
float rpm = (duty_cycle - 0.094) * (702.5 - 342.5) / (0.91 - 0.094) + 342.5;
// Only change when you get the same value twice consecutively
// to avoid changing multiple times when in transition.
if (rpm == m_pwm_current_rpm[1] && m_pwm_current_rpm[1] != m_pwm_current_rpm[0])
{
logerror("PWM index %3d duty cycle %5.1f%% rpm %f\n", internal_index, 100*duty_cycle, rpm);
if (m_cur_floppy && m_cur_floppy->type() == OAD34V)
{
m_iwm->sync();
m_cur_floppy->set_rpm(rpm);
}
}
m_pwm_current_rpm[0] = m_pwm_current_rpm[1];
m_pwm_current_rpm[1] = rpm;
m_pwm_count_1 = 0;
m_pwm_count_total = 0;
}
}
void mac128_state::scsi_irq_w(int state)
{
}
void mac128_state::scsi_drq_w(int state)
{
m_scsi_drq = state;
}
void mac128_state::scsi_berr_w(uint8_t data)
{
m_maincpu->trigger_bus_error();
}
uint16_t mac128_state::macplus_scsi_r(offs_t offset, uint16_t mem_mask)
{
int reg = (offset >> 3) & 0xf;
// logerror("macplus_scsi_r: offset %x mask %x\n", offset, mem_mask);
if (ACCESSING_BITS_0_7)
{
if ((offset >= 0x100) && (m_scsi_drq))
{
return m_ncr5380->dma_r();
}
return m_ncr5380->read(reg);
}
if ((offset >= 0x100) && (m_scsi_drq))
{
return u16(m_ncr5380->dma_r()) << 8;
}
return u16(m_ncr5380->read(reg)) << 8;
}
void mac128_state::macplus_scsi_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
int reg = (offset >> 3) & 0xf;
// logerror("macplus_scsi_w: data %x offset %x mask %x\n", data, offset, mem_mask);
// here we can take advantage of 68000 byte smearing
if ((offset >= 0x100) && (m_scsi_drq))
{
m_ncr5380->dma_w(data & 0xff);
}
m_ncr5380->write(reg, data & 0xff);
}
uint16_t mac128_state::macse_scsi_r(offs_t offset, uint16_t mem_mask)
{
int reg = (offset>>3) & 0xf;
// logerror("macplus_scsi_r: offset %x mask %x\n", offset, mem_mask);
return m_scsihelp->read_wrapper(BIT(offset, 8), reg)<<8;
}
void mac128_state::macse_scsi_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
int reg = (offset>>3) & 0xf;
// logerror("macplus_scsi_w: data %x offset %x mask %x\n", data, offset, mem_mask);
m_scsihelp->write_wrapper(BIT(offset, 8), reg, data>>8);
}
void mac128_state::scc_mouse_irq(int x, int y)
{
// DCD lines are active low in hardware but active high to software
if (x)
{
m_scc->dcda_w(m_mouse_last[0] ? 1 : 0);
if (x < 0)
{
m_mouse_bit[0] = m_mouse_last[0] ? 0 : 1;
}
else
{
m_mouse_bit[0] = m_mouse_last[0] ? 1 : 0;
}
m_mouse_last[0] = !m_mouse_last[0];
}
if (y)
{
m_scc->dcdb_w(m_mouse_last[1] ? 1 : 0);
if (y < 0)
{
m_mouse_bit[1] = m_mouse_last[1] ? 0 : 1;
}
else
{
m_mouse_bit[1] = m_mouse_last[1] ? 1 : 0;
}
m_mouse_last[1] = !m_mouse_last[1];
}
}
uint16_t mac128_state::mac_iwm_r(offs_t offset, uint16_t mem_mask)
{
uint16_t result = m_iwm->read((offset >> 8) & 0xf);
return (result << 8) | result;
}
void mac128_state::mac_iwm_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
if (ACCESSING_BITS_0_7)
m_iwm->write((offset >> 8) & 0xf, data & 0xff);
else
m_iwm->write((offset >> 8) & 0xf, data>>8);
}
void mac128_state::mac_via_irq(int state)
{
/* interrupt the 68k (level 1) */
set_via_interrupt(state);
}
uint16_t mac128_state::mac_via_r(offs_t offset)
{
uint16_t data;
offset >>= 8;
offset &= 0x0f;
data = m_via->read(offset);
return (data & 0xff) | (data << 8);
}
void mac128_state::mac_via_w(offs_t offset, uint16_t data)
{
offset >>= 8;
offset &= 0x0f;
m_via->write(offset, (data >> 8) & 0xff);
}
void mac128_state::mac_autovector_w(offs_t offset, uint16_t data)
{
/* This should throw an exception */
/* Not yet implemented */
}
uint16_t mac128_state::mac_autovector_r(offs_t offset)
{
/* This should throw an exception */
/* Not yet implemented */
return 0;
}
uint8_t mac128_state::mac_via_in_a()
{
return 0x81;
}
uint8_t mac128_state::mac_via_in_b()
{
int val = 0x40;
val |= m_mouse_bit[1] << 5; // Mouse Y2
val |= m_mouse_bit[0] << 4; // Mouse X2
val |= BIT(~m_mouse0->read(), 0) << 3;
val |= m_rtc->data_r();
// printf("%s VIA1 IN_B = %02x\n", machine().describe_context().c_str(), val);
return val;
}
uint8_t mac128_state::mac_via_in_b_se()
{
int val = 0;
if (!m_adb_irq_pending)
{
val |= 0x08;
}
val |= m_rtc->data_r();
// printf("%s VIA1 IN_B = %02x\n", machine().describe_context().c_str(), val);
return val;
}
void mac128_state::mac_via_out_a(uint8_t data)
{
// printf("%s VIA1 OUT A: %02x (PC %x)\n", machine().describe_context().c_str(), data);
//set_scc_waitrequest((data & 0x80) >> 7);
m_screen_buffer = (data & 0x40) >> 6;
int hdsel = BIT(data, 5);
if (hdsel != m_hdsel)
{
if (m_cur_floppy)
{
m_cur_floppy->ss_w(hdsel);
}
m_hdsel = hdsel;
}
m_main_buffer = ((data & 0x08) == 0x08) ? true : false;
m_snd_vol = data & 0x07;
update_volume();
/* Early Mac models had VIA A4 control overlaying. In the Mac SE and
* later models, overlay was set on reset, but cleared on the first
* access to the ROM's normal address space. */
if (((data & 0x10) >> 4) != m_overlay)
{
m_overlay = (data & 0x10) >> 4;
}
}
void mac128_state::mac_via_out_a_se(uint8_t data)
{
// printf("%s VIA OUT A: %02x (PC %x)\n", machine().describe_context().c_str(), data);
//set_scc_waitrequest((data & 0x80) >> 7);
m_screen_buffer = (data & 0x40) >> 6;
int hdsel = BIT(data, 5);
if (hdsel != m_hdsel)
{
if (m_cur_floppy)
{
m_cur_floppy->ss_w(hdsel);
}
m_hdsel = hdsel;
}
m_snd_vol = data & 0x07;
update_volume();
// on SE only this selects which floppy drive (0 = upper, 1 = lower)
if (m_drive_select != BIT(data, 4))
{
m_drive_select = BIT(data, 4);
devsel_se_w(m_devsel);
}
}
void mac128_state::mac_via_out_b(uint8_t data)
{
// printf("%s VIA1 OUT B: %02x\n", machine().describe_context().c_str(), data);
m_snd_enable = ((data & 0x80) == 0) ? true : false;
update_volume();
m_rtc->ce_w((data & 0x04)>>2);
m_rtc->data_w(data & 0x01);
m_rtc->clk_w((data >> 1) & 0x01);
}
void mac128_state::mac_via_out_b_se(uint8_t data)
{
// printf("%s VIA OUT B: %02x\n", machine().describe_context().c_str(), data);
m_snd_enable = ((data & 0x80) == 0) ? true : false;
update_volume();
m_scsiirq_enable = (data & 0x40) ? 0 : 1;
m_adbmodem->set_via_state((data & 0x30) >> 4);
m_rtc->ce_w((data & 0x04)>>2);
m_rtc->data_w(data & 0x01);
m_rtc->clk_w((data >> 1) & 0x01);
}
/* *************************************************************************
* Mouse
* *************************************************************************/
void mac128_state::mouse_callback()
{
// see if it moved in the x coord
const int new_mx = m_mouse1->read();
if (new_mx != m_mouse_last_m[0])
{
int diff = new_mx - m_mouse_last_m[0];
// check for wrap
if (diff > 0x80)
diff -= 0x100;
else if (diff < -0x80)
diff += 0x100;
m_mouse_count[0] += diff;
m_mouse_last_m[0] = new_mx;
}
// see if it moved in the y coord
const int new_my = m_mouse2->read();
if (new_my != m_mouse_last_m[1])
{
int diff = new_my - m_mouse_last_m[1];
// check for wrap
if (diff > 0x80)
diff -= 0x100;
else if (diff < -0x80)
diff += 0x100;
m_mouse_count[1] += diff;
m_mouse_last_m[1] = new_my;
}
// update any remaining count and then return
int x_needs_update = 0;
if (m_mouse_count[0] < 0)
{
m_mouse_count[0]++;
x_needs_update = -1;
}
else if (m_mouse_count[0])
{
m_mouse_count[0]--;
x_needs_update = 1;
}
int y_needs_update = 0;
if (m_mouse_count[1] < 0)
{
m_mouse_count[1]++;
y_needs_update = 1;
}
else if (m_mouse_count[1])
{
m_mouse_count[1]--;
y_needs_update = -1;
}
if (x_needs_update || y_needs_update)
{
// assert Port B External Interrupt on the SCC
scc_mouse_irq(x_needs_update, y_needs_update);
}
}
void mac128_state::mac_driver_init()
{
m_scsi_interrupt = 0;
memset(m_ram->pointer(), 0, m_ram->size());
}
#define MAC_MAIN_SCREEN_BUF_OFFSET (0x5900>>1)
#define MAC_ALT_SCREEN_BUF_OFFSET (0xD900>>1)
uint32_t mac128_state::screen_update_mac(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
uint32_t const video_base = m_ram_size - (m_screen_buffer ? MAC_MAIN_SCREEN_BUF_OFFSET : MAC_ALT_SCREEN_BUF_OFFSET);
uint16_t const *video_ram = (const uint16_t *) (m_ram_ptr + video_base);
for (int y = 0; y < MAC_V_VIS; y++)
{
uint16_t *const line = &bitmap.pix(y);
for (int x = 0; x < MAC_H_VIS; x += 16)
{
uint16_t const word = *(video_ram++);
for (int b = 0; b < 16; b++)
{
line[x + b] = (word >> (15 - b)) & 0x0001;
}
}
}
return 0;
}
void mac128_state::phases_w(uint8_t phases)
{
if (m_cur_floppy)
m_cur_floppy->seek_phase_w(phases);
}
void mac128_state::devsel_w(uint8_t devsel)
{
if (devsel == 1)
m_cur_floppy = m_floppy[0]->get_device();
else if (devsel == 2)
m_cur_floppy = m_floppy[1]->get_device();
else
m_cur_floppy = nullptr;
m_iwm->set_floppy(m_cur_floppy);
if (m_cur_floppy)
{
m_cur_floppy->ss_w(m_hdsel);
if (m_cur_floppy->type() == OAD34V)
m_cur_floppy->set_rpm(m_pwm_current_rpm[1]);
}