Browse Source

ym2151: remove timer code, use int16_t for samples

vampirefrog 1 year ago
parent
commit
2a0219ed18
2 changed files with 20 additions and 227 deletions
  1. 16
    197
      ym2151.c
  2. 4
    30
      ym2151.h

+ 16
- 197
ym2151.c View File

@@ -41,15 +41,9 @@
41 41
 
42 42
 #define TL_RES_LEN      (256) /* 8 bits addressing (real chip) */
43 43
 
44
-#if (SAMPLE_BITS==16)
45
-	#define FINAL_SH    (1)
46
-	#define MAXOUT      (+32767)
47
-	#define MINOUT      (-32768)
48
-#else
49
-	#define FINAL_SH    (8)
50
-	#define MAXOUT      (+127)
51
-	#define MINOUT      (-128)
52
-#endif
44
+#define FINAL_SH    (1)
45
+#define MAXOUT      (+32767)
46
+#define MINOUT      (-32768)
53 47
 
54 48
 /*  TL_TAB_LEN is calculated as:
55 49
 *   13 - sinus amplitude bits     (Y axis)
@@ -381,8 +375,6 @@ static void init_chip_tables(struct ym2151 *chip) {
381 375
 	int i,j;
382 376
 	double mult,phaseinc,Hz;
383 377
 	double scaler;
384
-	//attotime pom;
385
-	double pom;
386 378
 
387 379
 	scaler = ( (double)chip->clock / 64.0 ) / ( (double)chip->sampfreq );
388 380
 
@@ -438,33 +430,6 @@ static void init_chip_tables(struct ym2151 *chip) {
438 430
 		}
439 431
 	}
440 432
 
441
-
442
-	/* calculate timers' deltas */
443
-	/* User's Manual pages 15,16  */
444
-	mult = (1<<TIMER_SH);
445
-	for (i=0; i<1024; i++) {
446
-		/* ASG 980324: changed to compute both tim_A_tab and timer_A_time */
447
-		//pom= attotime_mul(ATTOTIME_IN_HZ(chip->clock), 64 * (1024 - i));
448
-		pom = ((double)64 * (1024 - i) / chip->clock);
449
-		#ifdef USE_MAME_TIMERS
450
-			chip->timer_A_time[i] = pom;
451
-		#else
452
-			//chip->tim_A_tab[i] = attotime_to_double(pom) * (double)chip->sampfreq * mult;  /* number of samples that timer period takes (fixed point) */
453
-			chip->tim_A_tab[i] = pom * (double)chip->sampfreq * mult;  /* number of samples that timer period takes (fixed point) */
454
-		#endif
455
-	}
456
-	for (i=0; i<256; i++) {
457
-		/* ASG 980324: changed to compute both tim_B_tab and timer_B_time */
458
-		//pom= attotime_mul(ATTOTIME_IN_HZ(chip->clock), 1024 * (256 - i));
459
-		pom = ((double)1024 * (256 - i) / chip->clock);
460
-		#ifdef USE_MAME_TIMERS
461
-			chip->timer_B_time[i] = pom;
462
-		#else
463
-			//chip->tim_B_tab[i] = attotime_to_double(pom) * (double)chip->sampfreq * mult;  /* number of samples that timer period takes (fixed point) */
464
-			chip->tim_B_tab[i] = pom * (double)chip->sampfreq * mult;  /* number of samples that timer period takes (fixed point) */
465
-		#endif
466
-	}
467
-
468 433
 	/* calculate noise periods table */
469 434
 	scaler = ( (double)chip->clock / 64.0 ) / ( (double)chip->sampfreq );
470 435
 	for (i=0; i<32; i++) {
@@ -719,94 +684,15 @@ void ym2151_write_reg(struct ym2151 *chip, int r, int v) {
719 684
 			break;
720 685
 
721 686
 		case 0x10:  /* timer A hi */
722
-			chip->timer_A_index = (chip->timer_A_index & 0x003) | (v<<2);
723 687
 			break;
724 688
 
725 689
 		case 0x11:  /* timer A low */
726
-			chip->timer_A_index = (chip->timer_A_index & 0x3fc) | (v & 3);
727 690
 			break;
728 691
 
729 692
 		case 0x12:  /* timer B */
730
-			chip->timer_B_index = v;
731 693
 			break;
732 694
 
733 695
 		case 0x14:  /* CSM, irq flag reset, irq enable, timer start/stop */
734
-
735
-			chip->irq_enable = v;   /* bit 3-timer B, bit 2-timer A, bit 7 - CSM */
736
-
737
-			if (v&0x10) /* reset timer A irq flag */
738
-			{
739
-#ifdef USE_MAME_TIMERS
740
-				chip->status &= ~1;
741
-				timer_set(chip->device->machine, attotime_zero,chip,0,irqAoff_callback);
742
-#else
743
-				//int oldstate = chip->status & 3;
744
-				chip->status &= ~1;
745
-				//if ((oldstate==1) && (chip->irqhandler)) (*chip->irqhandler)(chip->device, 0);
746
-#endif
747
-			}
748
-
749
-			if (v&0x20) /* reset timer B irq flag */
750
-			{
751
-#ifdef USE_MAME_TIMERS
752
-				chip->status &= ~2;
753
-				timer_set(chip->device->machine, attotime_zero,chip,0,irqBoff_callback);
754
-#else
755
-				//int oldstate = chip->status & 3;
756
-				chip->status &= ~2;
757
-				//if ((oldstate==2) && (chip->irqhandler)) (*chip->irqhandler)(chip->device, 0);
758
-#endif
759
-			}
760
-
761
-			if (v&0x02){    /* load and start timer B */
762
-				#ifdef USE_MAME_TIMERS
763
-				/* ASG 980324: added a real timer */
764
-				/* start timer _only_ if it wasn't already started (it will reload time value next round) */
765
-					if (!timer_enable(chip->timer_B, 1))
766
-					{
767
-						timer_adjust_oneshot(chip->timer_B, chip->timer_B_time[ chip->timer_B_index ], 0);
768
-						chip->timer_B_index_old = chip->timer_B_index;
769
-					}
770
-				#else
771
-					if (!chip->tim_B)
772
-					{
773
-						chip->tim_B = 1;
774
-						chip->tim_B_val = chip->tim_B_tab[ chip->timer_B_index ];
775
-					}
776
-				#endif
777
-			}else{      /* stop timer B */
778
-				#ifdef USE_MAME_TIMERS
779
-				/* ASG 980324: added a real timer */
780
-					timer_enable(chip->timer_B, 0);
781
-				#else
782
-					chip->tim_B = 0;
783
-				#endif
784
-			}
785
-
786
-			if (v&0x01){    /* load and start timer A */
787
-				#ifdef USE_MAME_TIMERS
788
-				/* ASG 980324: added a real timer */
789
-				/* start timer _only_ if it wasn't already started (it will reload time value next round) */
790
-					if (!timer_enable(chip->timer_A, 1))
791
-					{
792
-						timer_adjust_oneshot(chip->timer_A, chip->timer_A_time[ chip->timer_A_index ], 0);
793
-						chip->timer_A_index_old = chip->timer_A_index;
794
-					}
795
-				#else
796
-					if (!chip->tim_A)
797
-					{
798
-						chip->tim_A = 1;
799
-						chip->tim_A_val = chip->tim_A_tab[ chip->timer_A_index ];
800
-					}
801
-				#endif
802
-			}else{      /* stop timer A */
803
-				#ifdef USE_MAME_TIMERS
804
-				/* ASG 980324: added a real timer */
805
-					timer_enable(chip->timer_A, 0);
806
-				#else
807
-					chip->tim_A = 0;
808
-				#endif
809
-			}
810 696
 			break;
811 697
 
812 698
 		case 0x18:  /* LFO frequency */
@@ -1030,14 +916,6 @@ void ym2151_init(struct ym2151 *chip, int clock, int rate) {
1030 916
 	chip->eg_timer_add  = (1<<EG_SH)  * (clock/64.0) / chip->sampfreq;
1031 917
 	chip->eg_timer_overflow = ( 3 ) * (1<<EG_SH);
1032 918
 
1033
-#ifdef USE_MAME_TIMERS
1034
-	/* this must be done _before_ a call to ym2151_reset_chip() */
1035
-	chip->timer_A = timer_alloc(device->machine, timer_callback_a, chip);
1036
-	chip->timer_B = timer_alloc(device->machine, timer_callback_b, chip);
1037
-#else
1038
-	chip->tim_A      = 0;
1039
-	chip->tim_B      = 0;
1040
-#endif
1041 919
 	for (chn = 0; chn < 8; chn ++)
1042 920
 		chip->Muted[chn] = 0x00;
1043 921
 }
@@ -1073,22 +951,6 @@ void ym2151_reset_chip(struct ym2151 *chip) {
1073 951
 
1074 952
 	chip->test= 0;
1075 953
 
1076
-	chip->irq_enable = 0;
1077
-#ifdef USE_MAME_TIMERS
1078
-	/* ASG 980324 -- reset the timers before writing to the registers */
1079
-	timer_enable(chip->timer_A, 0);
1080
-	timer_enable(chip->timer_B, 0);
1081
-#else
1082
-	chip->tim_A      = 0;
1083
-	chip->tim_B      = 0;
1084
-	chip->tim_A_val  = 0;
1085
-	chip->tim_B_val  = 0;
1086
-#endif
1087
-	chip->timer_A_index = 0;
1088
-	chip->timer_B_index = 0;
1089
-	chip->timer_A_index_old = 0;
1090
-	chip->timer_B_index_old = 0;
1091
-
1092 954
 	chip->noise     = 0;
1093 955
 	chip->noise_rng = 0;
1094 956
 	chip->noise_p   = 0;
@@ -1686,31 +1548,10 @@ static void advance(struct ym2151 *chip) {
1686 1548
 *   '**buffers' is table of pointers to the buffers: left and right
1687 1549
 *   'length' is the number of samples that should be generated
1688 1550
 */
1689
-void ym2151_update_one(struct ym2151 *chip, SAMP **buffers, int length) {
1690
-	int i;
1691
-	signed int outl,outr;
1692
-	SAMP *bufL, *bufR;
1693
-
1694
-	bufL = buffers[0];
1695
-	bufR = buffers[1];
1696
-
1697
-#ifdef USE_MAME_TIMERS
1698
-		/* ASG 980324 - handled by real timers now */
1699
-#else
1700
-	if(chip->tim_B) {
1701
-		chip->tim_B_val -= (length << TIMER_SH);
1702
-		if(chip->tim_B_val <= 0) {
1703
-			chip->tim_B_val += chip->tim_B_tab[chip->timer_B_index];
1704
-			if(chip->irq_enable & 0x08) {
1705
-				//int oldstate = chip->status & 3;
1706
-				chip->status |= 2;
1707
-				//if ((!oldstate) && (chip->irqhandler)) (*chip->irqhandler)(chip->device, 1);
1708
-			}
1709
-		}
1710
-	}
1711
-#endif
1551
+void ym2151_update_one(struct ym2151 *chip, int16_t **buffers, int samples) {
1552
+	int16_t *bufL = buffers[0], *bufR = buffers[1];
1712 1553
 
1713
-	for(i=0; i<length; i++) {
1554
+	while(samples) {
1714 1555
 		advance_eg(chip);
1715 1556
 
1716 1557
 		chip->chanout[0] = 0;
@@ -1731,8 +1572,8 @@ void ym2151_update_one(struct ym2151 *chip, SAMP **buffers, int length) {
1731 1572
 		chan_calc(chip, 6);
1732 1573
 		chan7_calc(chip);
1733 1574
 
1734
-		outl = chip->chanout[0] & chip->pan[0];
1735
-		outr = chip->chanout[0] & chip->pan[1];
1575
+		int outl = chip->chanout[0] & chip->pan[0];
1576
+		int outr = chip->chanout[0] & chip->pan[1];
1736 1577
 		outl += (chip->chanout[1] & chip->pan[2]);
1737 1578
 		outr += (chip->chanout[1] & chip->pan[3]);
1738 1579
 		outl += (chip->chanout[2] & chip->pan[4]);
@@ -1750,36 +1591,14 @@ void ym2151_update_one(struct ym2151 *chip, SAMP **buffers, int length) {
1750 1591
 
1751 1592
 		outl >>= FINAL_SH;
1752 1593
 		outr >>= FINAL_SH;
1753
-		double outld = outl / 32767.0;
1754
-		if(outld < -1.0) outld = 1.0;
1755
-		else if(outld > 1.0) outld = 1.0;
1756
-		else outld = outld - outld * outld * outld / 3;
1757
-		double outrd = outr / 32767.0;
1758
-		if(outrd < -1.0) outrd = 1.0;
1759
-		else if(outrd > 1.0) outrd = 1.0;
1760
-		else outrd = outrd - outrd * outrd * outrd / 3;
1761
-
1762
-		((SAMP*)bufL)[i] = (SAMP)(outld * 32767);
1763
-		((SAMP*)bufR)[i] = (SAMP)(outrd * 32767);
1764
-
1765
-#ifdef USE_MAME_TIMERS
1766
-		/* ASG 980324 - handled by real timers now */
1767
-#else
1768
-		/* calculate timer A */
1769
-		if(chip->tim_A) {
1770
-			chip->tim_A_val -= (1 << TIMER_SH);
1771
-			if(chip->tim_A_val <= 0) {
1772
-				chip->tim_A_val += chip->tim_A_tab[ chip->timer_A_index ];
1773
-				if(chip->irq_enable & 0x04) {
1774
-					//int oldstate = chip->status & 3;
1775
-					chip->status |= 1;
1776
-					//if ((!oldstate) && (chip->irqhandler)) (*chip->irqhandler)(chip->device, 1);
1777
-				}
1778
-				if (chip->irq_enable & 0x80)
1779
-					chip->csm_req = 2;  /* request KEY ON / KEY OFF sequence */
1780
-			}
1781
-		}
1782
-#endif
1594
+		if(outl > 32767) outl = 32767;
1595
+		if(outl < -32768) outl = -32768;
1596
+		if(outr > 32767) outr = 32767;
1597
+		if(outr < -32768) outr = -32768;
1598
+		*bufL++ = outl;
1599
+		*bufR++ = outr;
1600
+		samples--;
1601
+
1783 1602
 		advance(chip);
1784 1603
 	}
1785 1604
 }

+ 4
- 30
ym2151.h View File

@@ -31,12 +31,6 @@
31 31
 #pragma once
32 32
 
33 33
 #include <stdint.h>
34
-#include "mamedef.h"
35
-
36
-/* 16- and 8-bit samples (signed) are supported*/
37
-#define SAMPLE_BITS 16
38
-
39
-typedef stream_sample_t SAMP;
40 34
 
41 35
 /* struct describing a single operator */
42 36
 struct ym2151_operator {
@@ -47,10 +41,10 @@ struct ym2151_operator {
47 41
 	uint32_t      dt1_i;                  /* DT1 index * 32 */
48 42
 	uint32_t      dt2;                    /* current DT2 (detune 2) value */
49 43
 
50
-	signed int *connect;                /* operator output 'direction' */
44
+	signed int   *connect;                /* operator output 'direction' */
51 45
 
52 46
 	/* only M1 (operator 0) is filled with this data: */
53
-	signed int *mem_connect;            /* where to put the delayed sample (MEM) */
47
+	signed int   *mem_connect;            /* where to put the delayed sample (MEM) */
54 48
 	int32_t       mem_value;              /* delayed sample (MEM) value */
55 49
 
56 50
 	/* channel specific data; note: each operator number 0 contains channel specific data */
@@ -90,7 +84,7 @@ struct ym2151_operator {
90 84
 };
91 85
 
92 86
 struct ym2151 {
93
-	struct ym2151_operator oper[32];    /* the 32 operators */
87
+	struct        ym2151_operator oper[32];    /* the 32 operators */
94 88
 
95 89
 	uint32_t      pan[16];                /* channels output masks (0xffffffff = enable) */
96 90
 	uint8_t       Muted[8];               /* used for muting */
@@ -126,26 +120,6 @@ struct ym2151 {
126 120
 	uint32_t      status;                 /* chip status (BUSY, IRQ Flags) */
127 121
 	uint8_t       connect[8];             /* channels connections */
128 122
 
129
-#ifdef USE_MAME_TIMERS
130
-	/* ASG 980324 -- added for tracking timers */
131
-	emu_timer   *timer_A;
132
-	emu_timer   *timer_B;
133
-	attotime    timer_A_time[1024];     /* timer A times for MAME */
134
-	attotime    timer_B_time[256];      /* timer B times for MAME */
135
-	int         irqlinestate;
136
-#else
137
-	uint8_t       tim_A;                  /* timer A enable (0-disabled) */
138
-	uint8_t       tim_B;                  /* timer B enable (0-disabled) */
139
-	int32_t       tim_A_val;              /* current value of timer A */
140
-	int32_t       tim_B_val;              /* current value of timer B */
141
-	uint32_t      tim_A_tab[1024];        /* timer A deltas */
142
-	uint32_t      tim_B_tab[256];         /* timer B deltas */
143
-#endif
144
-	uint32_t      timer_A_index;          /* timer A index */
145
-	uint32_t      timer_B_index;          /* timer B index */
146
-	uint32_t      timer_A_index_old;      /* timer A previous index */
147
-	uint32_t      timer_B_index_old;      /* timer B previous index */
148
-
149 123
 	/*  Frequency-deltas to get the closest frequency possible.
150 124
 	*   There are 11 octaves because of DT2 (max 950 cents over base frequency)
151 125
 	*   and LFO phase modulation (max 800 cents below AND over base frequency)
@@ -210,7 +184,7 @@ void ym2151_reset_chip(struct ym2151 *ym2151);
210 184
  * '**buffers' is table of pointers to the buffers: left and right
211 185
  * 'length' is the number of samples that should be generated
212 186
  */
213
-void ym2151_update_one(struct ym2151 *ym2151, SAMP **buffers, int length);
187
+void ym2151_update_one(struct ym2151 *ym2151, int16_t **buffers, int length);
214 188
 
215 189
 /* write 'v' to register 'r' on YM2151 */
216 190
 void ym2151_write_reg(struct ym2151 *ym2151, int r, int v);

Loading…
Cancel
Save