Previous 199869 Revisions Next

r22810 Monday 13th May, 2013 at 15:19:43 UTC by smf
modernised YMF278B [smf]
[src/emu/sound]ymf278b.c ymf278b.h
[src/mame/drivers]fuukifg3.c lordgun.c metro.c psikyo.c psikyo4.c psikyosh.c

trunk/src/mame/drivers/lordgun.c
r22809r22810
367367   AM_RANGE(0x3000, 0x3000) AM_READ(soundlatch2_byte_r )
368368   AM_RANGE(0x4000, 0x4000) AM_READ(soundlatch_byte_r )
369369   AM_RANGE(0x5000, 0x5000) AM_WRITENOP    // writes 03 then 07 at end of NMI
370   AM_RANGE(0x7000, 0x7000) AM_DEVREAD_LEGACY("ymf", ymf278b_r)
371   AM_RANGE(0x7000, 0x7005) AM_DEVWRITE_LEGACY("ymf", ymf278b_w)
370   AM_RANGE(0x7000, 0x7000) AM_DEVREAD("ymf", ymf278b_device, read)
371   AM_RANGE(0x7000, 0x7005) AM_DEVWRITE("ymf", ymf278b_device, write)
372372   AM_RANGE(0x7400, 0x7400) AM_DEVREADWRITE("oki", okim6295_device, read, write)
373373   AM_RANGE(0x7800, 0x7800) AM_DEVREADWRITE("oki2", okim6295_device, read, write)
374374ADDRESS_MAP_END
r22809r22810
701701MACHINE_CONFIG_END
702702
703703
704static const ymf278b_interface ymf278b_config =
705{
706   DEVCB_DRIVER_LINE_MEMBER(lordgun_state,soundirq)
707};
708
709704static MACHINE_CONFIG_START( aliencha, lordgun_state )
710705   MCFG_CPU_ADD("maincpu", M68000, XTAL_20MHz / 2)
711706   MCFG_CPU_PROGRAM_MAP(aliencha_map)
r22809r22810
734729   MCFG_SPEAKER_STANDARD_MONO("mono")
735730
736731   MCFG_SOUND_ADD("ymf", YMF278B, 26000000)            // ? 26MHz matches video (decrease for faster music tempo)
737   MCFG_SOUND_CONFIG(ymf278b_config)
732   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(lordgun_state, soundirq))
738733   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.5)
739734
740735   MCFG_OKIM6295_ADD("oki", XTAL_20MHz / 20, OKIM6295_PIN7_HIGH)   // ? 5MHz can't be right
trunk/src/mame/drivers/fuukifg3.c
r22809r22810
282282
283283WRITE8_MEMBER(fuuki32_state::snd_ymf278b_w)
284284{
285   ymf278b_w(machine().device("ymf1"), space, offset, data);
285   machine().device<ymf278b_device>("ymf1")->write(space, offset, data);
286286
287287   // also write to ymf262
288288   if (offset < 4)
r22809r22810
300300   ADDRESS_MAP_GLOBAL_MASK(0xff)
301301   AM_RANGE(0x00, 0x00) AM_WRITE(fuuki32_sound_bw_w)
302302   AM_RANGE(0x30, 0x30) AM_WRITENOP // leftover/unused nmi handler related
303   AM_RANGE(0x40, 0x45) AM_DEVREAD_LEGACY("ymf1", ymf278b_r) AM_WRITE(snd_ymf278b_w)
303   AM_RANGE(0x40, 0x45) AM_DEVREAD("ymf1", ymf278b_device, read) AM_WRITE(snd_ymf278b_w)
304304ADDRESS_MAP_END
305305
306306/***************************************************************************
r22809r22810
571571   m_soundcpu->set_input_line(0, state ? ASSERT_LINE : CLEAR_LINE);
572572}
573573
574static const ymf278b_interface fuuki32_ymf278b_interface =
575{
576   DEVCB_DRIVER_LINE_MEMBER(fuuki32_state,irqhandler)      /* irq */
577};
578
579574static MACHINE_CONFIG_START( fuuki32, fuuki32_state )
580575
581576   /* basic machine hardware */
r22809r22810
603598   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
604599
605600   MCFG_SOUND_ADD("ymf1", YMF278B, YMF278B_STD_CLOCK) // 33.8688MHz
606   MCFG_SOUND_CONFIG(fuuki32_ymf278b_interface)
601   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(fuuki32_state, irqhandler))
607602   MCFG_SOUND_ROUTE(0, "lspeaker", 0.50)
608603   MCFG_SOUND_ROUTE(1, "rspeaker", 0.50)
609604
trunk/src/mame/drivers/metro.c
r22809r22810
437437   m_portb = data;
438438}
439439
440static const ymf278b_interface ymf278b_config =
441{
442   DEVCB_DRIVER_LINE_MEMBER(metro_state,ymf278b_interrupt)
443};
444440
445
446441/***************************************************************************
447442
448443
r22809r22810
770765
771766static ADDRESS_MAP_START( balcube_map, AS_PROGRAM, 16, metro_state )
772767   AM_RANGE(0x000000, 0x07ffff) AM_ROM                                             // ROM
773   AM_RANGE(0x300000, 0x300001) AM_DEVREAD8_LEGACY("ymf", ymf278b_r, 0x00ff)       // Sound
774   AM_RANGE(0x300000, 0x30000b) AM_DEVWRITE8_LEGACY("ymf", ymf278b_w, 0x00ff)      // Sound
768   AM_RANGE(0x300000, 0x300001) AM_DEVREAD8("ymf", ymf278b_device, read, 0x00ff)   // Sound
769   AM_RANGE(0x300000, 0x30000b) AM_DEVWRITE8("ymf", ymf278b_device, write, 0x00ff) // Sound
775770   AM_RANGE(0x400000, 0x41ffff) AM_READ(balcube_dsw_r)                             // DSW x 3
776771   AM_RANGE(0x500000, 0x500001) AM_READ_PORT("IN0")                                // Inputs
777772   AM_RANGE(0x500002, 0x500003) AM_READ_PORT("IN1")                                //
r22809r22810
829824   AM_RANGE(0x200006, 0x200007) AM_READNOP                                         //
830825   AM_RANGE(0x200002, 0x200009) AM_WRITE(metro_coin_lockout_4words_w)              // Coin Lockout
831826   AM_RANGE(0x300000, 0x31ffff) AM_READ(balcube_dsw_r)                             // DSW x 3
832   AM_RANGE(0x400000, 0x400001) AM_DEVREAD8_LEGACY("ymf", ymf278b_r, 0x00ff)       // Sound
833   AM_RANGE(0x400000, 0x40000b) AM_DEVWRITE8_LEGACY("ymf", ymf278b_w, 0x00ff)      // Sound
827   AM_RANGE(0x400000, 0x400001) AM_DEVREAD8("ymf", ymf278b_device, read, 0x00ff)   // Sound
828   AM_RANGE(0x400000, 0x40000b) AM_DEVWRITE8("ymf", ymf278b_device, write, 0x00ff) // Sound
834829   AM_RANGE(0xf00000, 0xf0ffff) AM_RAM AM_MIRROR(0x0f0000)                         // RAM (mirrored)
835830ADDRESS_MAP_END
836831
r22809r22810
841836
842837static ADDRESS_MAP_START( bangball_map, AS_PROGRAM, 16, metro_state )
843838   AM_RANGE(0x000000, 0x07ffff) AM_ROM                                             // ROM
844   AM_RANGE(0xb00000, 0xb00001) AM_DEVREAD8_LEGACY("ymf", ymf278b_r, 0x00ff)       // Sound
845   AM_RANGE(0xb00000, 0xb0000b) AM_DEVWRITE8_LEGACY("ymf", ymf278b_w, 0x00ff)      // Sound
839   AM_RANGE(0xb00000, 0xb00001) AM_DEVREAD8("ymf", ymf278b_device, read, 0x00ff)   // Sound
840   AM_RANGE(0xb00000, 0xb0000b) AM_DEVWRITE8("ymf", ymf278b_device, write, 0x00ff) // Sound
846841   AM_RANGE(0xc00000, 0xc1ffff) AM_READ(balcube_dsw_r)                             // DSW x 3
847842   AM_RANGE(0xd00000, 0xd00001) AM_READ_PORT("IN0")                                // Inputs
848843   AM_RANGE(0xd00002, 0xd00003) AM_READ_PORT("IN1")                                //
r22809r22810
900895   AM_RANGE(0x200006, 0x200007) AM_READ_PORT("IN2")                                //
901896   AM_RANGE(0x200002, 0x200009) AM_WRITE(metro_coin_lockout_4words_w)              // Coin Lockout
902897   AM_RANGE(0x300000, 0x31ffff) AM_READ(balcube_dsw_r)                             // read but ignored?
903   AM_RANGE(0x400000, 0x400001) AM_DEVREAD8_LEGACY("ymf", ymf278b_r, 0x00ff)       // Sound
904   AM_RANGE(0x400000, 0x40000b) AM_DEVWRITE8_LEGACY("ymf", ymf278b_w, 0x00ff)      //
898   AM_RANGE(0x400000, 0x400001) AM_DEVREAD8("ymf", ymf278b_device, read, 0x00ff)   // Sound
899   AM_RANGE(0x400000, 0x40000b) AM_DEVWRITE8("ymf", ymf278b_device, write, 0x00ff) //
905900   AM_RANGE(0xf00000, 0xf0ffff) AM_RAM AM_MIRROR(0x0f0000)                         // RAM (mirrored)
906901ADDRESS_MAP_END
907902
r22809r22810
935930   AM_RANGE(0x200006, 0x200007) AM_READNOP                                         //
936931   AM_RANGE(0x200002, 0x200009) AM_WRITE(metro_coin_lockout_4words_w)              // Coin Lockout
937932   AM_RANGE(0x300000, 0x31ffff) AM_READ(balcube_dsw_r)                             // 3 x DSW
938   AM_RANGE(0x400000, 0x400001) AM_DEVREAD8_LEGACY("ymf", ymf278b_r, 0x00ff)       // Sound
939   AM_RANGE(0x400000, 0x40000b) AM_DEVWRITE8_LEGACY("ymf", ymf278b_w, 0x00ff)      //
933   AM_RANGE(0x400000, 0x400001) AM_DEVREAD8("ymf", ymf278b_device, read, 0x00ff)   // Sound
934   AM_RANGE(0x400000, 0x40000b) AM_DEVWRITE8("ymf", ymf278b_device, write, 0x00ff) //
940935   AM_RANGE(0xf00000, 0xf0ffff) AM_RAM AM_MIRROR(0x0f0000)                         // RAM (mirrored)
941936ADDRESS_MAP_END
942937
r22809r22810
35933588   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
35943589
35953590   MCFG_SOUND_ADD("ymf", YMF278B, YMF278B_STD_CLOCK)
3596   MCFG_SOUND_CONFIG(ymf278b_config)
3591   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(metro_state, ymf278b_interrupt))
35973592   MCFG_SOUND_ROUTE(0, "lspeaker", 1.0)
35983593   MCFG_SOUND_ROUTE(1, "rspeaker", 1.0)
35993594MACHINE_CONFIG_END
r22809r22810
36263621   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
36273622
36283623   MCFG_SOUND_ADD("ymf", YMF278B, YMF278B_STD_CLOCK)
3629   MCFG_SOUND_CONFIG(ymf278b_config)
3624   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(metro_state, ymf278b_interrupt))
36303625   MCFG_SOUND_ROUTE(0, "lspeaker", 1.0)
36313626   MCFG_SOUND_ROUTE(1, "rspeaker", 1.0)
36323627MACHINE_CONFIG_END
r22809r22810
36593654   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
36603655
36613656   MCFG_SOUND_ADD("ymf", YMF278B, YMF278B_STD_CLOCK)
3662   MCFG_SOUND_CONFIG(ymf278b_config)
3657   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(metro_state, ymf278b_interrupt))
36633658   MCFG_SOUND_ROUTE(0, "lspeaker", 1.0)
36643659   MCFG_SOUND_ROUTE(1, "rspeaker", 1.0)
36653660MACHINE_CONFIG_END
r22809r22810
36923687   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
36933688
36943689   MCFG_SOUND_ADD("ymf", YMF278B, YMF278B_STD_CLOCK)
3695   MCFG_SOUND_CONFIG(ymf278b_config)
3690   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(metro_state, ymf278b_interrupt))
36963691   MCFG_SOUND_ROUTE(0, "lspeaker", 1.0)
36973692   MCFG_SOUND_ROUTE(1, "rspeaker", 1.0)
36983693MACHINE_CONFIG_END
r22809r22810
37253720   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
37263721
37273722   MCFG_SOUND_ADD("ymf", YMF278B, YMF278B_STD_CLOCK)
3728   MCFG_SOUND_CONFIG(ymf278b_config)
3723   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(metro_state, ymf278b_interrupt))
37293724   MCFG_SOUND_ROUTE(0, "lspeaker", 1.0)
37303725   MCFG_SOUND_ROUTE(1, "rspeaker", 1.0)
37313726MACHINE_CONFIG_END
trunk/src/mame/drivers/psikyo4.c
r22809r22810
362362   AM_RANGE(0x03003ffc, 0x03003fff) AM_WRITE(ps4_bgpen_2_dword_w) AM_SHARE("bgpen_2") // screen 2 clear colour
363363   AM_RANGE(0x03004000, 0x03005fff) AM_RAM_WRITE(ps4_paletteram32_RRRRRRRRGGGGGGGGBBBBBBBBxxxxxxxx_dword_w) AM_SHARE("paletteram") // palette
364364   AM_RANGE(0x03006000, 0x03007fff) AM_ROMBANK("bank2") // data for rom tests (gfx), data is controlled by vidreg
365   AM_RANGE(0x05000000, 0x05000007) AM_DEVREADWRITE8_LEGACY("ymf", ymf278b_r, ymf278b_w, 0xffffffff)
365   AM_RANGE(0x05000000, 0x05000007) AM_DEVREADWRITE8("ymf", ymf278b_device, read, write, 0xffffffff)
366366   AM_RANGE(0x05800000, 0x05800003) AM_READ_PORT("P1_P2")
367367   AM_RANGE(0x05800004, 0x05800007) AM_READ_PORT("P3_P4")
368368   AM_RANGE(0x05800008, 0x0580000b) AM_WRITEONLY AM_SHARE("io_select") // Used by Mahjong games to choose input (also maps normal loderndf inputs to offsets)
r22809r22810
648648   m_maincpu->set_input_line(12, state ? ASSERT_LINE : CLEAR_LINE);
649649}
650650
651static const ymf278b_interface ymf278b_config =
652{
653   DEVCB_DRIVER_LINE_MEMBER(psikyo4_state,irqhandler)
654};
655651
656
657652void psikyo4_state::machine_start()
658653{
659654   save_item(NAME(m_oldbrt1));
r22809r22810
701696   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
702697
703698   MCFG_SOUND_ADD("ymf", YMF278B, MASTER_CLOCK/2)
704   MCFG_SOUND_CONFIG(ymf278b_config)
699   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(psikyo4_state, irqhandler))
705700   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "lspeaker", 1.0)
706701   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "rspeaker", 1.0)
707702MACHINE_CONFIG_END
trunk/src/mame/drivers/psikyo.c
r22809r22810
447447   ADDRESS_MAP_GLOBAL_MASK(0xff)
448448   AM_RANGE(0x00, 0x00) AM_WRITE(gunbird_sound_bankswitch_w)
449449   AM_RANGE(0x02, 0x03) AM_WRITENOP
450   AM_RANGE(0x08, 0x0d) AM_DEVREADWRITE_LEGACY("ymf", ymf278b_r, ymf278b_w)
450   AM_RANGE(0x08, 0x0d) AM_DEVREADWRITE("ymf", ymf278b_device, read, write)
451451   AM_RANGE(0x10, 0x10) AM_READ(psikyo_soundlatch_r)
452452   AM_RANGE(0x18, 0x18) AM_WRITE(psikyo_clear_nmi_w)
453453ADDRESS_MAP_END
r22809r22810
11531153   m_audiocpu->set_input_line(0, state ? ASSERT_LINE : CLEAR_LINE);
11541154}
11551155
1156static const ymf278b_interface ymf278b_config =
1157{
1158   DEVCB_DRIVER_LINE_MEMBER(psikyo_state,irqhandler)
1159};
1160
11611156static MACHINE_CONFIG_START( s1945, psikyo_state )
11621157
11631158   /* basic machine hardware */
r22809r22810
11901185   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
11911186
11921187   MCFG_SOUND_ADD("ymf", YMF278B, YMF278B_STD_CLOCK)
1193   MCFG_SOUND_CONFIG(ymf278b_config)
1188   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(psikyo_state, irqhandler))
11941189   MCFG_SOUND_ROUTE(0, "lspeaker", 1.0)
11951190   MCFG_SOUND_ROUTE(1, "rspeaker", 1.0)
11961191MACHINE_CONFIG_END
trunk/src/mame/drivers/psikyosh.c
r22809r22810
522522// rom mapping
523523   AM_RANGE(0x04060000, 0x0407ffff) AM_ROMBANK("bank2") // data for rom tests (gfx) (Mirrored?)
524524// sound chip
525   AM_RANGE(0x05000000, 0x05000007) AM_DEVREADWRITE8_LEGACY("ymf", ymf278b_r, ymf278b_w, 0xffffffff)
525   AM_RANGE(0x05000000, 0x05000007) AM_DEVREADWRITE8("ymf", ymf278b_device, read, write, 0xffffffff)
526526// inputs/eeprom
527527   AM_RANGE(0x05800000, 0x05800003) AM_READ_PORT("INPUTS")
528528   AM_RANGE(0x05800004, 0x05800007) AM_READWRITE(psh_eeprom_r, psh_eeprom_w)
r22809r22810
538538   AM_RANGE(0x03000000, 0x03000003) AM_READ_PORT("INPUTS")
539539   AM_RANGE(0x03000004, 0x03000007) AM_READWRITE(psh_eeprom_r, psh_eeprom_w)
540540// sound chip
541   AM_RANGE(0x03100000, 0x03100007) AM_DEVREADWRITE8_LEGACY("ymf", ymf278b_r, ymf278b_w, 0xffffffff)
541   AM_RANGE(0x03100000, 0x03100007) AM_DEVREADWRITE8("ymf", ymf278b_device, read, write, 0xffffffff)
542542// video chip
543543   AM_RANGE(0x04000000, 0x04003fff) AM_RAM AM_SHARE("spriteram") // video banks0-7 (sprites and sprite list)
544544   AM_RANGE(0x04004000, 0x0400ffff) AM_RAM AM_SHARE("bgram") // video banks 7-0x1f (backgrounds and other effects)
r22809r22810
785785   m_maincpu->set_input_line(12, state ? ASSERT_LINE : CLEAR_LINE);
786786}
787787
788static const ymf278b_interface ymf278b_config =
789{
790   DEVCB_DRIVER_LINE_MEMBER(psikyosh_state,irqhandler)
791};
792788
793
794789void psikyosh_state::machine_start()
795790{
796791   membank("bank2")->configure_entries(0, 0x1000, memregion("gfx1")->base(), 0x20000);
r22809r22810
827822   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
828823
829824   MCFG_SOUND_ADD("ymf", YMF278B, MASTER_CLOCK/2)
830   MCFG_SOUND_CONFIG(ymf278b_config)
825   MCFG_YMF278B_IRQ_HANDLER(WRITELINE(psikyosh_state, irqhandler))
831826   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "lspeaker", 1.0)
832827   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "rspeaker", 1.0)
833828MACHINE_CONFIG_END
trunk/src/emu/sound/ymf278b.c
r22809r22810
7171#define VERBOSE 0
7272#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
7373
74struct YMF278BChip;
75
76struct YMF278BSlot
74void ymf278b_device::write_memory(UINT32 offset, UINT8 data)
7775{
78   INT16 wave;     /* wavetable number */
79   INT16 F_NUMBER; /* frequency */
80   INT8 octave;    /* octave */
81   INT8 preverb;   /* pseudo-reverb */
82   INT8 DAMP;      /* damping */
83   INT8 CH;        /* output channel */
84   INT8 LD;        /* level direct */
85   INT8 TL;        /* total level */
86   INT8 pan;       /* panpot */
87   INT8 LFO;       /* LFO */
88   INT8 VIB;       /* vibrato */
89   INT8 AM;        /* tremolo */
90
91   INT8 AR;        /* attack rate */
92   INT8 D1R;       /* decay 1 rate */
93   INT8 DL;        /* decay level */
94   INT8 D2R;       /* decay 2 rate */
95   INT8 RC;        /* rate correction */
96   INT8 RR;        /* release rate */
97
98   UINT32 step;    /* fixed-point frequency step */
99   UINT64 stepptr; /* fixed-point pointer into the sample */
100
101   INT8 active;    /* channel is playing */
102   INT8 KEY_ON;    /* slot keyed on */
103   INT8 bits;      /* width of the samples */
104   UINT32 startaddr;
105   UINT32 loopaddr;
106   UINT32 endaddr;
107
108   int env_step;
109   UINT32 env_vol;
110   UINT32 env_vol_step;
111   UINT32 env_vol_lim;
112   INT8 env_preverb;
113
114   int num;        /* slot number (for debug only) */
115   YMF278BChip *chip;  /* pointer back to parent chip */
116};
117
118struct YMF278BChip
119{
120   UINT8 pcmregs[256];
121   YMF278BSlot slots[24];
122   INT8 wavetblhdr;
123   INT8 memmode;
124   INT32 memadr;
125
126   UINT8 status_busy, status_ld;
127   emu_timer *timer_busy;
128   emu_timer *timer_ld;
129   UINT8 exp;
130
131   INT32 fm_l, fm_r;
132   INT32 pcm_l, pcm_r;
133
134   attotime timer_base;
135   UINT8 timer_a_count, timer_b_count;
136   UINT8 enable, current_irq;
137   emu_timer *timer_a, *timer_b;
138   int irq_line;
139
140   UINT8 port_C, port_AB, lastport;
141   devcb_resolved_write_line irq_callback;
142   device_t *device;
143
144   const UINT8 *rom;
145   UINT32 romsize;
146   int clock;
147
148   // precomputed tables
149   UINT32 lut_ar[64];              // attack rate
150   UINT32 lut_dr[64];              // decay rate
151   INT32 volume[256*4];            // precalculated attenuation values with some margin for envelope and pan levels
152   int pan_left[16],pan_right[16]; // pan volume offsets
153   INT32 mix_level[8];
154
155   sound_stream * stream;
156};
157
158INLINE YMF278BChip *get_safe_token(device_t *device)
159{
160   assert(device != NULL);
161   assert(device->type() == YMF278B);
162   return (YMF278BChip *)downcast<ymf278b_device *>(device)->token();
163}
164
165static void ymf278b_write_memory(YMF278BChip *chip, UINT32 offset, UINT8 data)
166{
16776   logerror("YMF278B:  Memory write %02x to %x\n", data, offset);
16877}
16978
170INLINE UINT8 ymf278b_read_memory(YMF278BChip *chip, UINT32 offset)
79UINT8 ymf278b_device::read_memory(UINT32 offset)
17180{
172   if (offset >= chip->romsize)
81   if (offset >= m_romsize)
17382   {
17483      // logerror("YMF278B:  Memory read overflow %x\n", offset);
17584      return 0xff;
17685   }
177   return chip->rom[offset];
86   return m_rom[offset];
17887}
17988
18089
18190/**************************************************************************/
18291
183static int ymf278b_compute_rate(YMF278BSlot *slot, int val)
92int ymf278b_device::compute_rate(YMF278BSlot *slot, int val)
18493{
18594   int res, oct;
18695
r22809r22810
206115   return res;
207116}
208117
209static UINT32 ymf278_compute_decay_env_vol_step(YMF278BSlot *slot, int val)
118UINT32 ymf278b_device::compute_decay_env_vol_step(YMF278BSlot *slot, int val)
210119{
211120   int rate;
212121   UINT32 res;
r22809r22810
221130      rate = 5;
222131   }
223132   else
224      rate = ymf278b_compute_rate(slot, val);
133      rate = compute_rate(slot, val);
225134
226135   if (rate < 4)
227136      res = 0;
228137   else
229      res = (256U<<23) / slot->chip->lut_dr[rate];
138      res = (256U<<23) / m_lut_dr[rate];
230139
231140   return res;
232141}
233142
234static void ymf278b_compute_freq_step(YMF278BSlot *slot)
143void ymf278b_device::compute_freq_step(YMF278BSlot *slot)
235144{
236145   UINT32 step;
237146   int oct;
r22809r22810
244153   slot->step = step >> 3;
245154}
246155
247static void ymf278b_compute_envelope(YMF278BSlot *slot)
156void ymf278b_device::compute_envelope(YMF278BSlot *slot)
248157{
249158   switch (slot->env_step)
250159   {
r22809r22810
252161      case 0:
253162      {
254163         // Attack
255         int rate = ymf278b_compute_rate(slot, slot->AR);
164         int rate = compute_rate(slot, slot->AR);
256165         slot->env_vol = 256U<<23;
257166         slot->env_vol_lim = (256U<<23) - 1;
258167
r22809r22810
262171            LOG(("YMF278B: Attack skipped - "));
263172            slot->env_vol = 0;
264173            slot->env_step++;
265            ymf278b_compute_envelope(slot);
174            compute_envelope(slot);
266175         }
267176         else if (rate<4)
268177         {
r22809r22810
271180         else
272181         {
273182            // NOTE: attack rate is linear here, but datasheet shows a smooth curve
274            LOG(("YMF278B: Attack, val = %d, rate = %d, delay = %g\n", slot->AR, rate, slot->chip->lut_ar[rate]*1000.0));
275            slot->env_vol_step = ~((256U<<23) / slot->chip->lut_ar[rate]);
183            LOG(("YMF278B: Attack, val = %d, rate = %d, delay = %g\n", slot->AR, rate, m_lut_ar[rate]*1000.0));
184            slot->env_vol_step = ~((256U<<23) / m_lut_ar[rate]);
276185         }
277186
278187         break;
r22809r22810
282191      case 1:
283192         if(slot->DL)
284193         {
285            LOG(("YMF278B: Decay step 1, dl=%d, val = %d rate = %d, delay = %g, PRVB = %d, DAMP = %d\n", slot->DL, slot->D1R, ymf278b_compute_rate(slot, slot->D1R), slot->chip->lut_dr[ymf278b_compute_rate(slot, slot->D1R)]*1000.0, slot->preverb, slot->DAMP));
286            slot->env_vol_step = ymf278_compute_decay_env_vol_step(slot, slot->D1R);
194            LOG(("YMF278B: Decay step 1, dl=%d, val = %d rate = %d, delay = %g, PRVB = %d, DAMP = %d\n", slot->DL, slot->D1R, compute_rate(slot, slot->D1R), m_lut_dr[compute_rate(slot, slot->D1R)]*1000.0, slot->preverb, slot->DAMP));
195            slot->env_vol_step = compute_decay_env_vol_step(slot, slot->D1R);
287196            slot->env_vol_lim = (slot->DL*8)<<23;
288197         }
289198         else
290199         {
291200            LOG(("YMF278B: Decay 1 skipped - "));
292201            slot->env_step++;
293            ymf278b_compute_envelope(slot);
202            compute_envelope(slot);
294203         }
295204
296205         break;
297206
298207      // Decay 2
299208      case 2:
300         LOG(("YMF278B: Decay step 2, val = %d, rate = %d, delay = %g, , PRVB = %d, DAMP = %d, current vol = %d\n", slot->D2R, ymf278b_compute_rate(slot, slot->D2R), slot->chip->lut_dr[ymf278b_compute_rate(slot, slot->D2R)]*1000.0, slot->preverb, slot->DAMP, slot->env_vol >> 23));
301         slot->env_vol_step = ymf278_compute_decay_env_vol_step(slot, slot->D2R);
209         LOG(("YMF278B: Decay step 2, val = %d, rate = %d, delay = %g, , PRVB = %d, DAMP = %d, current vol = %d\n", slot->D2R, compute_rate(slot, slot->D2R), m_lut_dr[compute_rate(slot, slot->D2R)]*1000.0, slot->preverb, slot->DAMP, slot->env_vol >> 23));
210         slot->env_vol_step = compute_decay_env_vol_step(slot, slot->D2R);
302211         slot->env_vol_lim = 256U<<23;
303212         break;
304213
r22809r22810
313222
314223      // Release
315224      case 4:
316         LOG(("YMF278B: Release, val = %d, rate = %d, delay = %g, PRVB = %d, DAMP = %d\n", slot->RR, ymf278b_compute_rate(slot, slot->RR), slot->chip->lut_dr[ymf278b_compute_rate(slot, slot->RR)]*1000.0, slot->preverb, slot->DAMP));
317         slot->env_vol_step = ymf278_compute_decay_env_vol_step(slot, slot->RR);
225         LOG(("YMF278B: Release, val = %d, rate = %d, delay = %g, PRVB = %d, DAMP = %d\n", slot->RR, compute_rate(slot, slot->RR), m_lut_dr[compute_rate(slot, slot->RR)]*1000.0, slot->preverb, slot->DAMP));
226         slot->env_vol_step = compute_decay_env_vol_step(slot, slot->RR);
318227         slot->env_vol_lim = 256U<<23;
319228         break;
320229
r22809r22810
331240   }
332241}
333242
334static STREAM_UPDATE( ymf278b_pcm_update )
243//-------------------------------------------------
244//  sound_stream_update - handle a stream update
245//-------------------------------------------------
246
247void ymf278b_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
335248{
336   YMF278BChip *chip = (YMF278BChip *)param;
337249   int i, j;
338250   YMF278BSlot *slot = NULL;
339251   INT16 sample = 0;
r22809r22810
345257
346258   for (i = 0; i < 24; i++)
347259   {
348      slot = &chip->slots[i];
260      slot = &m_slots[i];
349261
350262      if (slot->active)
351263      {
r22809r22810
364276            {
365277               // 8 bit
366278               case 0:
367                  sample = ymf278b_read_memory(chip, slot->startaddr + (slot->stepptr>>16))<<8;
279                  sample = read_memory(slot->startaddr + (slot->stepptr>>16))<<8;
368280                  break;
369281
370282               // 12 bit
371283               case 1:
372284                  if (slot->stepptr & 0x10000)
373                     sample = ymf278b_read_memory(chip, slot->startaddr + (slot->stepptr>>17)*3+2)<<8 |
374                        (ymf278b_read_memory(chip, slot->startaddr + (slot->stepptr>>17)*3+1) << 4 & 0xf0);
285                     sample = read_memory(slot->startaddr + (slot->stepptr>>17)*3+2)<<8 |
286                        (read_memory(slot->startaddr + (slot->stepptr>>17)*3+1) << 4 & 0xf0);
375287                  else
376                     sample = ymf278b_read_memory(chip, slot->startaddr + (slot->stepptr>>17)*3)<<8 |
377                        (ymf278b_read_memory(chip, slot->startaddr + (slot->stepptr>>17)*3+1) & 0xf0);
288                     sample = read_memory(slot->startaddr + (slot->stepptr>>17)*3)<<8 |
289                        (read_memory(slot->startaddr + (slot->stepptr>>17)*3+1) & 0xf0);
378290                  break;
379291
380292               // 16 bit
381293               case 2:
382                  sample = ymf278b_read_memory(chip, slot->startaddr + ((slot->stepptr>>16)*2))<<8 |
383                     ymf278b_read_memory(chip, slot->startaddr + ((slot->stepptr>>16)*2)+1);
294                  sample = read_memory(slot->startaddr + ((slot->stepptr>>16)*2))<<8 |
295                     read_memory(slot->startaddr + ((slot->stepptr>>16)*2)+1);
384296                  break;
385297
386298               // ?? bit, effect is unknown, datasheet says it's prohibited
r22809r22810
389301                  break;
390302            }
391303
392            *mixp++ += (sample * chip->volume[slot->TL+chip->pan_left [slot->pan]+(slot->env_vol>>23)])>>17;
393            *mixp++ += (sample * chip->volume[slot->TL+chip->pan_right[slot->pan]+(slot->env_vol>>23)])>>17;
304            *mixp++ += (sample * m_volume[slot->TL+m_pan_left [slot->pan]+(slot->env_vol>>23)])>>17;
305            *mixp++ += (sample * m_volume[slot->TL+m_pan_right[slot->pan]+(slot->env_vol>>23)])>>17;
394306
395307            // update frequency
396308            slot->stepptr += slot->step;
r22809r22810
400312            if (((INT32)(slot->env_vol - slot->env_vol_lim)) >= 0)
401313            {
402314               slot->env_step++;
403               ymf278b_compute_envelope(slot);
315               compute_envelope(slot);
404316            }
405317            else if (slot->preverb && !slot->env_preverb && slot->env_step && slot->env_vol > ((6*8)<<23))
406               ymf278b_compute_envelope(slot);
318               compute_envelope(slot);
407319         }
408320      }
409321   }
410322
411323   mixp = mix;
412   vl = chip->mix_level[chip->pcm_l];
413   vr = chip->mix_level[chip->pcm_r];
324   vl = m_mix_level[m_pcm_l];
325   vr = m_mix_level[m_pcm_r];
414326   for (i = 0; i < samples; i++)
415327   {
416328      outputs[0][i] = (*mixp++ * vl) >> 16;
r22809r22810
418330   }
419331}
420332
421static void ymf278b_irq_check(running_machine &machine, YMF278BChip *chip)
333void ymf278b_device::irq_check()
422334{
423   int prev_line = chip->irq_line;
424   chip->irq_line = chip->current_irq ? ASSERT_LINE : CLEAR_LINE;
425   if(chip->irq_line != prev_line && !chip->irq_callback.isnull())
426      chip->irq_callback(chip->irq_line);
335   int prev_line = m_irq_line;
336   m_irq_line = m_current_irq ? ASSERT_LINE : CLEAR_LINE;
337   if(m_irq_line != prev_line && !m_irq_handler.isnull())
338      m_irq_handler(m_irq_line);
427339}
428340
429static TIMER_CALLBACK( ymf278b_timer_a_tick )
341enum
430342{
431   YMF278BChip *chip = (YMF278BChip *)ptr;
432   if(!(chip->enable & 0x40))
433   {
434      chip->current_irq |= 0x40;
435      ymf278b_irq_check(machine, chip);
436   }
437}
343   TIMER_A = 0,
344   TIMER_B,
345   TIMER_BUSY_CLEAR,
346   TIMER_LD_CLEAR
347};
438348
439static TIMER_CALLBACK( ymf278b_timer_b_tick )
349void ymf278b_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
440350{
441   YMF278BChip *chip = (YMF278BChip *)ptr;
442   if(!(chip->enable & 0x20))
351   switch(id)
443352   {
444      chip->current_irq |= 0x20;
445      ymf278b_irq_check(machine, chip);
353   case TIMER_A:
354      if(!(m_enable & 0x40))
355      {
356         m_current_irq |= 0x40;
357         irq_check();
358      }
359      break;
360
361   case TIMER_B:
362      if(!(m_enable & 0x20))
363      {
364         m_current_irq |= 0x20;
365         irq_check();
366      }
367      break;
368
369   case TIMER_BUSY_CLEAR:
370      m_status_busy = 0;
371      break;
372
373   case TIMER_LD_CLEAR:
374      m_status_ld = 0;
375      break;
446376   }
447377}
448378
449379
450380/**************************************************************************/
451381
452static void ymf278b_A_w(running_machine &machine, YMF278BChip *chip, UINT8 reg, UINT8 data)
382void ymf278b_device::A_w(UINT8 reg, UINT8 data)
453383{
454384   // FM register array 0 (compatible with YMF262)
455385   switch(reg)
r22809r22810
461391
462392      // timer a count
463393      case 0x02:
464         if (data != chip->timer_a_count)
394         if (data != m_timer_a_count)
465395         {
466            chip->timer_a_count = data;
396            m_timer_a_count = data;
467397
468398            // change period, ~80.8us * t
469            if (chip->enable & 1)
470               chip->timer_a->adjust(chip->timer_a->remaining(), 0, chip->timer_base * (256-data) * 4);
399            if (m_enable & 1)
400               m_timer_a->adjust(m_timer_a->remaining(), 0, m_timer_base * (256-data) * 4);
471401         }
472402         break;
473403
474404      // timer b count
475405      case 0x03:
476         if (data != chip->timer_b_count)
406         if (data != m_timer_b_count)
477407         {
478            chip->timer_b_count = data;
408            m_timer_b_count = data;
479409
480410            // change period, ~323.1us * t
481            if (chip->enable & 2)
482               chip->timer_b->adjust(chip->timer_b->remaining(), 0, chip->timer_base * (256-data) * 16);
411            if (m_enable & 2)
412               m_timer_b->adjust(m_timer_b->remaining(), 0, m_timer_base * (256-data) * 16);
483413         }
484414         break;
485415
486416      // timer control
487417      case 0x04:
488418         if(data & 0x80)
489            chip->current_irq = 0;
419            m_current_irq = 0;
490420         else
491421         {
492422            // reset timers
493            if((chip->enable ^ data) & 1)
423            if((m_enable ^ data) & 1)
494424            {
495               attotime period = (data & 1) ? chip->timer_base * (256-chip->timer_a_count) * 4 : attotime::never;
496               chip->timer_a->adjust(period, 0, period);
425               attotime period = (data & 1) ? m_timer_base * (256-m_timer_a_count) * 4 : attotime::never;
426               m_timer_a->adjust(period, 0, period);
497427            }
498            if((chip->enable ^ data) & 2)
428            if((m_enable ^ data) & 2)
499429            {
500               attotime period = (data & 2) ? chip->timer_base * (256-chip->timer_b_count) * 16 : attotime::never;
501               chip->timer_b->adjust(period, 0, period);
430               attotime period = (data & 2) ? m_timer_base * (256-m_timer_b_count) * 16 : attotime::never;
431               m_timer_b->adjust(period, 0, period);
502432            }
503433
504            chip->enable = data;
505            chip->current_irq &= ~data;
434            m_enable = data;
435            m_current_irq &= ~data;
506436         }
507         ymf278b_irq_check(machine, chip);
437         irq_check();
508438         break;
509439
510440      default:
r22809r22810
513443   }
514444}
515445
516static void ymf278b_B_w(YMF278BChip *chip, UINT8 reg, UINT8 data)
446void ymf278b_device::B_w(UINT8 reg, UINT8 data)
517447{
518448   // FM register array 1 (compatible with YMF262)
519449   switch(reg)
r22809r22810
525455
526456      // expansion register (NEW2/NEW)
527457      case 0x05:
528         chip->exp = data;
458         m_exp = data;
529459         break;
530460
531461      default:
r22809r22810
534464   }
535465}
536466
537static TIMER_CALLBACK( ymf278b_timer_ld_clear )
467void ymf278b_device::retrigger_note(YMF278BSlot *slot)
538468{
539   YMF278BChip *chip = (YMF278BChip *)ptr;
540   chip->status_ld = 0;
541}
542
543static void ymf278b_retrigger_note(YMF278BSlot *slot)
544{
545469   // activate channel
546470   if (slot->octave != 8)
547471      slot->active = 1;
r22809r22810
551475   slot->env_step = 0;
552476   slot->env_preverb = 0;
553477
554   ymf278b_compute_freq_step(slot);
555   ymf278b_compute_envelope(slot);
478   compute_freq_step(slot);
479   compute_envelope(slot);
556480}
557481
558static void ymf278b_C_w(YMF278BChip *chip, UINT8 reg, UINT8 data, int init)
482void ymf278b_device::C_w(UINT8 reg, UINT8 data, int init)
559483{
560484   if (!init)
561485   {
562486      // PCM regs are only accessible if NEW2 is set
563      if (~chip->exp & 2)
487      if (~m_exp & 2)
564488         return;
565489
566      chip->stream->update();
490      m_stream->update();
567491   }
568492
569493   // Handle slot registers specifically
r22809r22810
572496      YMF278BSlot *slot = NULL;
573497      int snum;
574498      snum = (reg-8) % 24;
575      slot = &chip->slots[snum];
499      slot = &m_slots[snum];
576500      switch((reg-8) / 24)
577501      {
578502         case 0:
r22809r22810
586510            slot->wave |= data;
587511
588512            // load wavetable header
589            if(slot->wave < 384 || !chip->wavetblhdr)
513            if(slot->wave < 384 || !m_wavetblhdr)
590514               offset = slot->wave * 12;
591515            else
592               offset = chip->wavetblhdr*0x80000 + (slot->wave - 384) * 12;
516               offset = m_wavetblhdr*0x80000 + (slot->wave - 384) * 12;
593517            for (i = 0; i < 12; i++)
594               p[i] = ymf278b_read_memory(chip, offset+i);
518               p[i] = read_memory(offset+i);
595519
596520            slot->bits = (p[0]&0xc0)>>6;
597521            slot->startaddr = (p[2] | (p[1]<<8) | ((p[0]&0x3f)<<16));
r22809r22810
602526
603527            // copy internal registers data
604528            for (i = 7; i < 12; i++)
605               ymf278b_C_w(chip, 8 + snum + (i-2) * 24, p[i], 1);
529               C_w(8 + snum + (i-2) * 24, p[i], 1);
606530
607531            // status register LD bit is on for approx 300us
608            chip->status_ld = 1;
532            m_status_ld = 1;
609533            period = attotime::from_usec(300);
610            if (chip->clock != YMF278B_STD_CLOCK)
611               period = (period * chip->clock) / YMF278B_STD_CLOCK;
612            chip->timer_ld->adjust(period);
534            if (m_clock != YMF278B_STD_CLOCK)
535               period = (period * m_clock) / YMF278B_STD_CLOCK;
536            m_timer_ld->adjust(period);
613537
614538            // retrigger if key is on
615539            if (slot->KEY_ON)
616               ymf278b_retrigger_note(slot);
540               retrigger_note(slot);
617541            else if (slot->active)
618542            {
619543               // deactivate channel
620544               slot->env_step = 5;
621               ymf278b_compute_envelope(slot);
545               compute_envelope(slot);
622546            }
623547
624548            break;
r22809r22810
629553            slot->wave |= ((data&0x1)<<8);
630554            slot->F_NUMBER &= 0x380;
631555            slot->F_NUMBER |= (data>>1);
632            if (slot->active && (data ^ chip->pcmregs[reg]) & 0xfe)
556            if (slot->active && (data ^ m_pcmregs[reg]) & 0xfe)
633557            {
634               ymf278b_compute_freq_step(slot);
635               ymf278b_compute_envelope(slot);
558               compute_freq_step(slot);
559               compute_envelope(slot);
636560            }
637561            break;
638562
r22809r22810
641565            slot->F_NUMBER |= ((data&0x07)<<7);
642566            slot->preverb = (data&0x8)>>3;
643567            slot->octave = (data&0xf0)>>4;
644            if (data != chip->pcmregs[reg])
568            if (data != m_pcmregs[reg])
645569            {
646570               // channel goes off if octave is set to -8 (datasheet says it's prohibited)
647               // (it is ok if this activates the channel while it was off: ymf278b_compute_envelope will reset it again if needed)
571               // (it is ok if this activates the channel while it was off: compute_envelope will reset it again if needed)
648572               slot->active = (slot->octave != 8);
649573
650574               if (slot->active)
651575               {
652576                  slot->env_preverb = 0;
653                  ymf278b_compute_freq_step(slot);
654                  ymf278b_compute_envelope(slot);
577                  compute_freq_step(slot);
578                  compute_envelope(slot);
655579               }
656580            }
657581            break;
r22809r22810
675599               // don't retrigger if key was already on
676600               if (slot->KEY_ON)
677601               {
678                  if ((data ^ chip->pcmregs[reg]) & 0x40)
679                     ymf278b_compute_envelope(slot);
602                  if ((data ^ m_pcmregs[reg]) & 0x40)
603                     compute_envelope(slot);
680604
681605                  break;
682606               }
683607
684               ymf278b_retrigger_note(slot);
608               retrigger_note(slot);
685609            }
686610            else if (slot->active)
687611            {
688612               // release
689613               slot->env_step = 4;
690               ymf278b_compute_envelope(slot);
614               compute_envelope(slot);
691615            }
692616            slot->KEY_ON = (data&0x80)>>7;
693617            break;
r22809r22810
701625         case 6:
702626            slot->AR = data>>4;
703627            slot->D1R = data&0xf;
704            if (slot->active && data != chip->pcmregs[reg])
705               ymf278b_compute_envelope(slot);
628            if (slot->active && data != m_pcmregs[reg])
629               compute_envelope(slot);
706630            break;
707631
708632         case 7:
709633            slot->DL = data>>4;
710634            slot->D2R = data&0xf;
711            if (slot->active && data != chip->pcmregs[reg])
712               ymf278b_compute_envelope(slot);
635            if (slot->active && data != m_pcmregs[reg])
636               compute_envelope(slot);
713637            break;
714638
715639         case 8:
716640            slot->RC = data>>4;
717641            slot->RR = data&0xf;
718            if (slot->active && data != chip->pcmregs[reg])
719               ymf278b_compute_envelope(slot);
642            if (slot->active && data != m_pcmregs[reg])
643               compute_envelope(slot);
720644            break;
721645
722646         case 9:
r22809r22810
736660            break;
737661
738662         case 0x02:
739            chip->wavetblhdr = (data>>2)&0x7;
740            chip->memmode = data&3;
663            m_wavetblhdr = (data>>2)&0x7;
664            m_memmode = data&3;
741665            break;
742666
743667         case 0x03:
r22809r22810
745669            break;
746670         case 0x05:
747671            // set memory address
748            chip->memadr = (chip->pcmregs[3] & 0x3f) << 16 | chip->pcmregs[4] << 8 | data;
672            m_memadr = (m_pcmregs[3] & 0x3f) << 16 | m_pcmregs[4] << 8 | data;
749673            break;
750674
751675         case 0x06:
752676            // memory data (ignored, we don't support RAM)
753            ymf278b_write_memory(chip, chip->memadr, data);
754            chip->memadr = (chip->memadr + 1) & 0x3fffff;
677            write_memory(m_memadr, data);
678            m_memadr = (m_memadr + 1) & 0x3fffff;
755679            break;
756680
757681         case 0x07:
758682            break; // unused
759683
760684         case 0xf8:
761            chip->fm_l = data & 0x7;
762            chip->fm_r = (data>>3)&0x7;
685            m_fm_l = data & 0x7;
686            m_fm_r = (data>>3)&0x7;
763687            break;
764688
765689         case 0xf9:
766            chip->pcm_l = data & 0x7;
767            chip->pcm_r = (data>>3)&0x7;
690            m_pcm_l = data & 0x7;
691            m_pcm_r = (data>>3)&0x7;
768692            break;
769693
770694         default:
r22809r22810
773697      }
774698   }
775699
776   chip->pcmregs[reg] = data;
700   m_pcmregs[reg] = data;
777701}
778702
779static TIMER_CALLBACK( ymf278b_timer_busy_clear )
703void ymf278b_device::timer_busy_start(int is_pcm)
780704{
781   YMF278BChip *chip = (YMF278BChip *)ptr;
782   chip->status_busy = 0;
783}
784
785static void ymf278b_timer_busy_start(YMF278BChip *chip, int is_pcm)
786{
787705   // status register BUSY bit is on for 56(FM) or 88(PCM) cycles
788   chip->status_busy = 1;
789   chip->timer_busy->adjust(attotime::from_hz(chip->clock / (is_pcm ? 88 : 56)));
706   m_status_busy = 1;
707   m_timer_busy->adjust(attotime::from_hz(m_clock / (is_pcm ? 88 : 56)));
790708}
791709
792WRITE8_DEVICE_HANDLER( ymf278b_w )
710WRITE8_MEMBER( ymf278b_device::write )
793711{
794   YMF278BChip *chip = get_safe_token(device);
795
796712   switch (offset)
797713   {
798714      case 0:
799715      case 2:
800         ymf278b_timer_busy_start(chip, 0);
801         chip->port_AB = data;
802         chip->lastport = offset>>1 & 1;
716         timer_busy_start(0);
717         m_port_AB = data;
718         m_lastport = offset>>1 & 1;
803719         break;
804720
805721      case 1:
806722      case 3:
807         ymf278b_timer_busy_start(chip, 0);
808         if (chip->lastport) ymf278b_B_w(chip, chip->port_AB, data);
809         else ymf278b_A_w(device->machine(), chip, chip->port_AB, data);
723         timer_busy_start(0);
724         if (m_lastport) B_w(m_port_AB, data);
725         else A_w(m_port_AB, data);
810726         break;
811727
812728      case 4:
813         ymf278b_timer_busy_start(chip, 1);
814         chip->port_C = data;
729         timer_busy_start(1);
730         m_port_C = data;
815731         break;
816732
817733      case 5:
818         ymf278b_timer_busy_start(chip, 1);
819         ymf278b_C_w(chip, chip->port_C, data, 0);
734         timer_busy_start(1);
735         C_w(m_port_C, data, 0);
820736         break;
821737
822738      default:
823         logerror("%s: unexpected write at offset %X to ymf278b = %02X\n", device->machine().describe_context(), offset, data);
739         logerror("%s: unexpected write at offset %X to ymf278b = %02X\n", machine().describe_context(), offset, data);
824740         break;
825741   }
826742}
827743
828744
829READ8_DEVICE_HANDLER( ymf278b_r )
745READ8_MEMBER( ymf278b_device::read )
830746{
831   YMF278BChip *chip = get_safe_token(device);
832747   UINT8 ret = 0;
833748
834749   switch (offset)
r22809r22810
838753      {
839754         // bits 0 and 1 are only valid if NEW2 is set
840755         UINT8 newbits = 0;
841         if (chip->exp & 2)
842            newbits = (chip->status_ld << 1) | chip->status_busy;
756         if (m_exp & 2)
757            newbits = (m_status_ld << 1) | m_status_busy;
843758
844         ret = newbits | chip->current_irq | (chip->irq_line == ASSERT_LINE ? 0x80 : 0x00);
759         ret = newbits | m_current_irq | (m_irq_line == ASSERT_LINE ? 0x80 : 0x00);
845760         break;
846761      }
847762
r22809r22810
854769      // PCM regs
855770      case 5:
856771         // only accessible if NEW2 is set
857         if (~chip->exp & 2)
772         if (~m_exp & 2)
858773            break;
859774
860         switch (chip->port_C)
775         switch (m_port_C)
861776         {
862777            // special cases
863778            case 2:
864               ret = (chip->pcmregs[chip->port_C] & 0x1f) | 0x20; // device ID in upper bits
779               ret = (m_pcmregs[m_port_C] & 0x1f) | 0x20; // device ID in upper bits
865780               break;
866781            case 6:
867               ret = ymf278b_read_memory(chip, chip->memadr);
868               chip->memadr = (chip->memadr + 1) & 0x3fffff;
782               ret = read_memory(m_memadr);
783               m_memadr = (m_memadr + 1) & 0x3fffff;
869784               break;
870785
871786            default:
872               ret = chip->pcmregs[chip->port_C];
787               ret = m_pcmregs[m_port_C];
873788               break;
874789         }
875790         break;
876791
877792      default:
878         logerror("%s: unexpected read at offset %X from ymf278b\n", device->machine().describe_context(), offset);
793         logerror("%s: unexpected read at offset %X from ymf278b\n", machine().describe_context(), offset);
879794         break;
880795   }
881796
r22809r22810
885800
886801/**************************************************************************/
887802
888static DEVICE_RESET( ymf278b )
803//-------------------------------------------------
804//  device_reset - device-specific reset
805//-------------------------------------------------
806
807void ymf278b_device::device_reset()
889808{
890   YMF278BChip *chip = get_safe_token(device);
891809   int i;
892810
893811   // clear registers
894812   for (i = 0; i <= 4; i++)
895      ymf278b_A_w(device->machine(), chip, i, 0);
896   ymf278b_B_w(chip, 5, 0);
813      A_w(i, 0);
814   B_w(5, 0);
897815   for (i = 0; i < 8; i++)
898      ymf278b_C_w(chip, i, 0, 1);
816      C_w(i, 0, 1);
899817   for (i = 0xff; i >= 8; i--)
900      ymf278b_C_w(chip, i, 0, 1);
901   ymf278b_C_w(chip, 0xf8, 0x1b, 1);
818      C_w(i, 0, 1);
819   C_w(0xf8, 0x1b, 1);
902820
903   chip->port_AB = chip->port_C = 0;
904   chip->lastport = 0;
905   chip->memadr = 0;
821   m_port_AB = m_port_C = 0;
822   m_lastport = 0;
823   m_memadr = 0;
906824
907825   // init/silence channels
908826   for (i = 0; i < 24 ; i++)
909827   {
910      YMF278BSlot *slot = &chip->slots[i];
828      YMF278BSlot *slot = &m_slots[i];
911829
912830      slot->LFO = 0;
913831      slot->VIB = 0;
r22809r22810
924842      slot->endaddr = 0;
925843
926844      slot->env_step = 5;
927      ymf278b_compute_envelope(slot);
845      compute_envelope(slot);
928846   }
929847
930   chip->timer_a->reset();
931   chip->timer_b->reset();
932   chip->timer_busy->reset();  chip->status_busy = 0;
933   chip->timer_ld->reset();    chip->status_ld = 0;
848   m_timer_a->reset();
849   m_timer_b->reset();
850   m_timer_busy->reset();  m_status_busy = 0;
851   m_timer_ld->reset();    m_status_ld = 0;
934852
935   chip->irq_line = CLEAR_LINE;
853   m_irq_line = CLEAR_LINE;
936854}
937855
938static void ymf278b_init(device_t *device, YMF278BChip *chip, const devcb_write_line *cb)
856void ymf278b_device::precompute_rate_tables()
939857{
940858   int i;
941859
942   chip->rom = *device->region();
943   chip->romsize = device->region()->bytes();
944   chip->clock = device->clock();
945   chip->irq_callback.resolve(*cb, *device);
946
947   chip->timer_base = attotime::from_hz(chip->clock) * (19*36);
948   chip->timer_a = device->machine().scheduler().timer_alloc(FUNC(ymf278b_timer_a_tick), chip);
949   chip->timer_b = device->machine().scheduler().timer_alloc(FUNC(ymf278b_timer_b_tick), chip);
950   chip->timer_busy = device->machine().scheduler().timer_alloc(FUNC(ymf278b_timer_busy_clear), chip);
951   chip->timer_ld = device->machine().scheduler().timer_alloc(FUNC(ymf278b_timer_ld_clear), chip);
952
953   for (i = 0; i < 24; i++)
954   {
955      chip->slots[i].num = i;
956      chip->slots[i].chip = chip;
957   }
958}
959
960static void precompute_rate_tables(YMF278BChip *chip)
961{
962   int i;
963
964860   // decay rate
965861   for (i = 0; i < 64; i++)
966862   {
967863      if (i <= 3)
968         chip->lut_dr[i] = 0;
864         m_lut_dr[i] = 0;
969865      else if (i >= 60)
970         chip->lut_dr[i] = 15 << 4;
866         m_lut_dr[i] = 15 << 4;
971867      else
972         chip->lut_dr[i] = (15 << (21 - i / 4)) / (4 + i % 4);
868         m_lut_dr[i] = (15 << (21 - i / 4)) / (4 + i % 4);
973869   }
974870
975871   // attack rate (manual shows curve instead of linear though, so this is not entirely accurate)
976872   for (i = 0; i < 64; i++)
977873   {
978874      if (i <= 3 || i == 63)
979         chip->lut_ar[i] = 0;
875         m_lut_ar[i] = 0;
980876      else if (i >= 60)
981         chip->lut_ar[i] = 17;
877         m_lut_ar[i] = 17;
982878      else
983         chip->lut_ar[i] = (67 << (15 - i / 4)) / (4 + i % 4);
879         m_lut_ar[i] = (67 << (15 - i / 4)) / (4 + i % 4);
984880   }
985881}
986882
987static void ymf278b_register_save_state(device_t *device, YMF278BChip *chip)
883void ymf278b_device::register_save_state()
988884{
989885   int i;
990886
991   device->save_item(NAME(chip->pcmregs));
992   device->save_item(NAME(chip->wavetblhdr));
993   device->save_item(NAME(chip->memmode));
994   device->save_item(NAME(chip->memadr));
995   device->save_item(NAME(chip->status_busy));
996   device->save_item(NAME(chip->status_ld));
997   device->save_item(NAME(chip->exp));
998   device->save_item(NAME(chip->fm_l));
999   device->save_item(NAME(chip->fm_r));
1000   device->save_item(NAME(chip->pcm_l));
1001   device->save_item(NAME(chip->pcm_r));
1002   device->save_item(NAME(chip->timer_a_count));
1003   device->save_item(NAME(chip->timer_b_count));
1004   device->save_item(NAME(chip->enable));
1005   device->save_item(NAME(chip->current_irq));
1006   device->save_item(NAME(chip->irq_line));
1007   device->save_item(NAME(chip->port_AB));
1008   device->save_item(NAME(chip->port_C));
1009   device->save_item(NAME(chip->lastport));
887   save_item(NAME(m_pcmregs));
888   save_item(NAME(m_wavetblhdr));
889   save_item(NAME(m_memmode));
890   save_item(NAME(m_memadr));
891   save_item(NAME(m_status_busy));
892   save_item(NAME(m_status_ld));
893   save_item(NAME(m_exp));
894   save_item(NAME(m_fm_l));
895   save_item(NAME(m_fm_r));
896   save_item(NAME(m_pcm_l));
897   save_item(NAME(m_pcm_r));
898   save_item(NAME(m_timer_a_count));
899   save_item(NAME(m_timer_b_count));
900   save_item(NAME(m_enable));
901   save_item(NAME(m_current_irq));
902   save_item(NAME(m_irq_line));
903   save_item(NAME(m_port_AB));
904   save_item(NAME(m_port_C));
905   save_item(NAME(m_lastport));
1010906
1011907   for (i = 0; i < 24; ++i)
1012908   {
1013      device->save_item(NAME(chip->slots[i].wave), i);
1014      device->save_item(NAME(chip->slots[i].F_NUMBER), i);
1015      device->save_item(NAME(chip->slots[i].octave), i);
1016      device->save_item(NAME(chip->slots[i].preverb), i);
1017      device->save_item(NAME(chip->slots[i].DAMP), i);
1018      device->save_item(NAME(chip->slots[i].CH), i);
1019      device->save_item(NAME(chip->slots[i].LD), i);
1020      device->save_item(NAME(chip->slots[i].TL), i);
1021      device->save_item(NAME(chip->slots[i].pan), i);
1022      device->save_item(NAME(chip->slots[i].LFO), i);
1023      device->save_item(NAME(chip->slots[i].VIB), i);
1024      device->save_item(NAME(chip->slots[i].AM), i);
909      save_item(NAME(m_slots[i].wave), i);
910      save_item(NAME(m_slots[i].F_NUMBER), i);
911      save_item(NAME(m_slots[i].octave), i);
912      save_item(NAME(m_slots[i].preverb), i);
913      save_item(NAME(m_slots[i].DAMP), i);
914      save_item(NAME(m_slots[i].CH), i);
915      save_item(NAME(m_slots[i].LD), i);
916      save_item(NAME(m_slots[i].TL), i);
917      save_item(NAME(m_slots[i].pan), i);
918      save_item(NAME(m_slots[i].LFO), i);
919      save_item(NAME(m_slots[i].VIB), i);
920      save_item(NAME(m_slots[i].AM), i);
1025921
1026      device->save_item(NAME(chip->slots[i].AR), i);
1027      device->save_item(NAME(chip->slots[i].D1R), i);
1028      device->save_item(NAME(chip->slots[i].DL), i);
1029      device->save_item(NAME(chip->slots[i].D2R), i);
1030      device->save_item(NAME(chip->slots[i].RC), i);
1031      device->save_item(NAME(chip->slots[i].RR), i);
922      save_item(NAME(m_slots[i].AR), i);
923      save_item(NAME(m_slots[i].D1R), i);
924      save_item(NAME(m_slots[i].DL), i);
925      save_item(NAME(m_slots[i].D2R), i);
926      save_item(NAME(m_slots[i].RC), i);
927      save_item(NAME(m_slots[i].RR), i);
1032928
1033      device->save_item(NAME(chip->slots[i].step), i);
1034      device->save_item(NAME(chip->slots[i].stepptr), i);
929      save_item(NAME(m_slots[i].step), i);
930      save_item(NAME(m_slots[i].stepptr), i);
1035931
1036      device->save_item(NAME(chip->slots[i].active), i);
1037      device->save_item(NAME(chip->slots[i].KEY_ON), i);
1038      device->save_item(NAME(chip->slots[i].bits), i);
1039      device->save_item(NAME(chip->slots[i].startaddr), i);
1040      device->save_item(NAME(chip->slots[i].loopaddr), i);
1041      device->save_item(NAME(chip->slots[i].endaddr), i);
932      save_item(NAME(m_slots[i].active), i);
933      save_item(NAME(m_slots[i].KEY_ON), i);
934      save_item(NAME(m_slots[i].bits), i);
935      save_item(NAME(m_slots[i].startaddr), i);
936      save_item(NAME(m_slots[i].loopaddr), i);
937      save_item(NAME(m_slots[i].endaddr), i);
1042938
1043      device->save_item(NAME(chip->slots[i].env_step), i);
1044      device->save_item(NAME(chip->slots[i].env_vol), i);
1045      device->save_item(NAME(chip->slots[i].env_vol_step), i);
1046      device->save_item(NAME(chip->slots[i].env_vol_lim), i);
1047      device->save_item(NAME(chip->slots[i].env_preverb), i);
939      save_item(NAME(m_slots[i].env_step), i);
940      save_item(NAME(m_slots[i].env_vol), i);
941      save_item(NAME(m_slots[i].env_vol_step), i);
942      save_item(NAME(m_slots[i].env_vol_lim), i);
943      save_item(NAME(m_slots[i].env_preverb), i);
1048944   }
1049945}
1050946
1051static DEVICE_START( ymf278b )
947//-------------------------------------------------
948//  device_start - device-specific startup
949//-------------------------------------------------
950
951void ymf278b_device::device_start()
1052952{
1053   static const ymf278b_interface defintrf = { DEVCB_NULL };
1054   const ymf278b_interface *intf;
1055953   int i;
1056   YMF278BChip *chip = get_safe_token(device);
1057954
1058   chip->device = device;
1059   intf = (device->static_config() != NULL) ? (const ymf278b_interface *)device->static_config() : &defintrf;
955   m_rom = *region();
956   m_romsize = region()->bytes();
957   m_clock = clock();
958   m_irq_handler.resolve();
1060959
1061   ymf278b_init(device, chip, &intf->irq_callback);
1062   chip->stream = device->machine().sound().stream_alloc(*device, 0, 2, device->clock()/768, chip, ymf278b_pcm_update);
960   m_timer_base = attotime::from_hz(m_clock) * (19*36);
961   m_timer_a = timer_alloc(TIMER_A);
962   m_timer_b = timer_alloc(TIMER_B);
963   m_timer_busy = timer_alloc(TIMER_BUSY_CLEAR);
964   m_timer_ld = timer_alloc(TIMER_LD_CLEAR);
1063965
966   for (i = 0; i < 24; i++)
967   {
968      m_slots[i].num = i;
969   }
970
971   m_stream = machine().sound().stream_alloc(*this, 0, 2, clock()/768);
972
1064973   // rate tables
1065   precompute_rate_tables(chip);
974   precompute_rate_tables();
1066975
1067976   // Volume table, 1 = -0.375dB, 8 = -3dB, 256 = -96dB
1068977   for(i = 0; i < 256; i++)
1069      chip->volume[i] = 65536*pow(2.0, (-0.375/6)*i);
978      m_volume[i] = 65536*pow(2.0, (-0.375/6)*i);
1070979   for(i = 256; i < 256*4; i++)
1071      chip->volume[i] = 0;
980      m_volume[i] = 0;
1072981
1073982   // Pan values, units are -3dB, i.e. 8.
1074983   for(i = 0; i < 16; i++)
1075984   {
1076      chip->pan_left[i] = i < 7 ? i*8 : i < 9 ? 256 : 0;
1077      chip->pan_right[i] = i < 8 ? 0 : i < 10 ? 256 : (16-i)*8;
985      m_pan_left[i] = i < 7 ? i*8 : i < 9 ? 256 : 0;
986      m_pan_right[i] = i < 8 ? 0 : i < 10 ? 256 : (16-i)*8;
1078987   }
1079988
1080989   // Mixing levels, units are -3dB, and add some margin to avoid clipping
1081990   for(i=0; i<7; i++)
1082      chip->mix_level[i] = chip->volume[8*i+13];
1083   chip->mix_level[7] = 0;
991      m_mix_level[i] = m_volume[8*i+13];
992   m_mix_level[7] = 0;
1084993
1085994   // Register state for saving
1086   ymf278b_register_save_state(device, chip);
995   register_save_state();
1087996}
1088997
1089998
r22809r22810
10911000
10921001ymf278b_device::ymf278b_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
10931002   : device_t(mconfig, YMF278B, "YMF278B", tag, owner, clock),
1094      device_sound_interface(mconfig, *this)
1003      device_sound_interface(mconfig, *this),
1004      m_irq_handler(*this)
10951005{
1096   m_token = global_alloc_clear(YMF278BChip);
10971006}
10981007
10991008//-------------------------------------------------
r22809r22810
11051014void ymf278b_device::device_config_complete()
11061015{
11071016}
1108
1109//-------------------------------------------------
1110//  device_start - device-specific startup
1111//-------------------------------------------------
1112
1113void ymf278b_device::device_start()
1114{
1115   DEVICE_START_NAME( ymf278b )(this);
1116}
1117
1118//-------------------------------------------------
1119//  device_reset - device-specific reset
1120//-------------------------------------------------
1121
1122void ymf278b_device::device_reset()
1123{
1124   DEVICE_RESET_NAME( ymf278b )(this);
1125}
1126
1127//-------------------------------------------------
1128//  sound_stream_update - handle a stream update
1129//-------------------------------------------------
1130
1131void ymf278b_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
1132{
1133   // should never get here
1134   fatalerror("sound_stream_update called; not applicable to legacy sound devices\n");
1135}
trunk/src/emu/sound/ymf278b.h
r22809r22810
33#ifndef __YMF278B_H__
44#define __YMF278B_H__
55
6#include "devlegcy.h"
6#include "emu.h"
77
88#define YMF278B_STD_CLOCK (33868800)            /* standard clock for OPL4 */
99
10#define MCFG_YMF278B_IRQ_HANDLER(_devcb) \
11   devcb = &ymf278b_device::set_irq_handler(*device, DEVCB2_##_devcb);
1012
11struct ymf278b_interface
12{
13   devcb_write_line irq_callback;  /* irq callback */
14};
15
16DECLARE_READ8_DEVICE_HANDLER( ymf278b_r );
17DECLARE_WRITE8_DEVICE_HANDLER( ymf278b_w );
18
1913class ymf278b_device : public device_t,
2014                           public device_sound_interface
2115{
2216public:
2317   ymf278b_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
24   ~ymf278b_device() { global_free(m_token); }
2518
26   // access to legacy token
27   void *token() const { assert(m_token != NULL); return m_token; }
19   // static configuration helpers
20   template<class _Object> static devcb2_base &set_irq_handler(device_t &device, _Object object) { return downcast<ymf278b_device &>(device).m_irq_handler.set_callback(object); }
21
22   DECLARE_READ8_MEMBER( read );
23   DECLARE_WRITE8_MEMBER( write );
24
2825protected:
2926   // device-level overrides
3027   virtual void device_config_complete();
3128   virtual void device_start();
3229   virtual void device_reset();
3330
31   virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
32
3433   // sound stream update overrides
3534   virtual void sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples);
35
3636private:
37   struct YMF278BSlot
38   {
39      INT16 wave;     /* wavetable number */
40      INT16 F_NUMBER; /* frequency */
41      INT8 octave;    /* octave */
42      INT8 preverb;   /* pseudo-reverb */
43      INT8 DAMP;      /* damping */
44      INT8 CH;        /* output channel */
45      INT8 LD;        /* level direct */
46      INT8 TL;        /* total level */
47      INT8 pan;       /* panpot */
48      INT8 LFO;       /* LFO */
49      INT8 VIB;       /* vibrato */
50      INT8 AM;        /* tremolo */
51
52      INT8 AR;        /* attack rate */
53      INT8 D1R;       /* decay 1 rate */
54      INT8 DL;        /* decay level */
55      INT8 D2R;       /* decay 2 rate */
56      INT8 RC;        /* rate correction */
57      INT8 RR;        /* release rate */
58
59      UINT32 step;    /* fixed-point frequency step */
60      UINT64 stepptr; /* fixed-point pointer into the sample */
61
62      INT8 active;    /* channel is playing */
63      INT8 KEY_ON;    /* slot keyed on */
64      INT8 bits;      /* width of the samples */
65      UINT32 startaddr;
66      UINT32 loopaddr;
67      UINT32 endaddr;
68
69      int env_step;
70      UINT32 env_vol;
71      UINT32 env_vol_step;
72      UINT32 env_vol_lim;
73      INT8 env_preverb;
74
75      int num;        /* slot number (for debug only) */
76   };
77
78   void write_memory(UINT32 offset, UINT8 data);
79   UINT8 read_memory(UINT32 offset);
80   int compute_rate(YMF278BSlot *slot, int val);
81   UINT32 compute_decay_env_vol_step(YMF278BSlot *slot, int val);
82   void compute_freq_step(YMF278BSlot *slot);
83   void compute_envelope(YMF278BSlot *slot);
84   void irq_check();
85   void A_w(UINT8 reg, UINT8 data);
86   void B_w(UINT8 reg, UINT8 data);
87   void retrigger_note(YMF278BSlot *slot);
88   void C_w(UINT8 reg, UINT8 data, int init);
89   void timer_busy_start(int is_pcm);
90   void precompute_rate_tables();
91   void register_save_state();
92
3793   // internal state
38   void *m_token;
94   UINT8 m_pcmregs[256];
95   YMF278BSlot m_slots[24];
96   INT8 m_wavetblhdr;
97   INT8 m_memmode;
98   INT32 m_memadr;
99
100   UINT8 m_status_busy, m_status_ld;
101   emu_timer *m_timer_busy;
102   emu_timer *m_timer_ld;
103   UINT8 m_exp;
104
105   INT32 m_fm_l, m_fm_r;
106   INT32 m_pcm_l, m_pcm_r;
107
108   attotime m_timer_base;
109   UINT8 m_timer_a_count, m_timer_b_count;
110   UINT8 m_enable, m_current_irq;
111   int m_irq_line;
112
113   UINT8 m_port_C, m_port_AB, m_lastport;
114
115   // precomputed tables
116   UINT32 m_lut_ar[64];              // attack rate
117   UINT32 m_lut_dr[64];              // decay rate
118   INT32 m_volume[256*4];            // precalculated attenuation values with some margin for envelope and pan levels
119   int m_pan_left[16],m_pan_right[16]; // pan volume offsets
120   INT32 m_mix_level[8];
121
122   emu_timer *m_timer_a, *m_timer_b;
123   const UINT8 *m_rom;
124   UINT32 m_romsize;
125   int m_clock;
126
127   sound_stream * m_stream;
128   devcb2_write_line m_irq_handler;
39129};
40130
41131extern const device_type YMF278B;

Previous 199869 Revisions Next


© 1997-2024 The MAME Team