]> Shamusworld >> Repos - thunder/blob - src/ym2151.h
Added V63701, YM2151 emulation, more SDL 2 fixes.
[thunder] / src / ym2151.h
1 /*
2 **
3 ** File: ym2151.h -- header file for software implementation of YM-2151
4 **                                              FM Operator Type-M(OPM).
5 **
6 ** (C) Jarek Burczynski (s0246@goblin.pjwstk.waw.pl) September, 1st 1997
7 **
8 ** First beta release: December, 16th 1997
9 ** Version 1.0 beta release: March, 16th 1998
10 **
11 **
12 ** CAUTION !!! CAUTION !!!           H E L P   W A N T E D !!!
13 **
14 ** If you have some very technical information about YM-2151, or possibly you
15 ** own a computer (like X68000) which has YM-2151 built-in (so you could make
16 ** a few samples of real chip), and you would like to help:
17 **                  - PLEASE CONTACT ME ASAP!!! -
18 **
19 **
20 **
21 ** I would like to thank to Shigeharu Isoda without whom this project wouldn't
22 ** be possible to finish (possibly even start). He has been so kind and helped
23 ** me by scanning his YM2151 Japanese Manual first, and then by answering MANY
24 ** of questions I've asked (I can't read Japanese still, although I study
25 ** informatics at Polish-Japanese Institute here in Poland).
26 **
27 ** I would like to thank to nao (nao@ms6.hinet.net) also - he has been the one
28 ** who gave me some information about YM2151, and pointed me to Shigeharu.
29 **
30 ** Big thanks to Aaron Giles and Chris Hardy - they've made some samples of one
31 ** of my favourite arcade games so I could compare it to my emulator. That's
32 ** how I know there's still something I need to know about this chip.
33 **
34 ** Greetings to Ishmair (thanks for the datasheet !) who has inspired me to
35 ** write my own 2151 emu by telling me "... maybe with some somes and a lot of
36 ** luck ... in theory it's difficult ... " - wow that really was a challenge :)
37 **
38 */
39
40 #ifndef __YM2151_H__
41 #define __YM2151_H__
42
43 // Register IDs
44 #define YM_KON          (0x08)
45 #define YM_NOISE        (0x0F)
46
47 #define YM_CLOCKA1      (0x10)
48 #define YM_CLOCKA2      (0x11)
49 #define YM_CLOCKB       (0x12)
50 #define YM_CLOCKSET     (0x14)
51
52 #define YM_LFRQ         (0x18)
53 #define YM_PMD_AMD      (0x19)
54 #define YM_CT1_CT2_W    (0x1B)
55
56
57 #define YM_CONNECT_BASE (0x20) /*1 reg per FM channel*/
58 #define YM_KC_BASE      (0x28)
59 #define YM_KF_BASE      (0x30)
60 #define YM_PMS_AMS_BASE (0x38)
61
62 #define YM_DT1_MUL_BASE (0x40) /*1 reg per operator*/
63 #define YM_TL_BASE      (0x60)
64 #define YM_KS_AR_BASE   (0x80)
65 #define YM_AMS_D1R_BASE (0xA0)
66 #define YM_DT2_D2R_BASE (0xC0)
67 #define YM_D1L_RR_BASE  (0xE0)
68
69 typedef void SAMPLE;  /*Yep, 16 bit is supported, at last*/
70
71 typedef struct{      /*oscillator data */
72         signed int freq;                /*oscillator frequency (in indexes per sample) (fixed point)*/
73         signed int phase;               /*accumulated oscillator phase (in indexes)    (fixed point)*/
74
75         unsigned int TL;                /*this is output level, in fact     */
76         signed int volume;              /*oscillator volume (fixed point)   */
77         signed int attack_volume;       /*used for attack phase calculations*/
78
79         signed int OscilFB;             /*oscillator self feedback used only by operators 0*/
80
81         int D1L;
82         unsigned char AR;
83         unsigned char D1R;
84         unsigned char D2R;
85         unsigned char RR;
86         signed int delta_AR;    /*volume delta for attack  phase (fixed point)*/
87         signed int delta_D1R;   /*volume delta for decay   phase (fixed point)*/
88         signed int delta_D2R;   /*volume delta for sustain phase (fixed point)*/
89         signed int delta_RR;    /*volume delta for release phase (fixed point)*/
90         unsigned char state;    /*Envelope state: 0-attack(AR) 1-decay(D1R) 2-sustain(D2R) 3-release(RR) 4-finished*/
91 } OscilRec;
92
93
94 /* here's the virtual YM2151 */
95 typedef struct ym2151_f {
96     SAMPLE * Buf;       /* sound buffer */
97     int bufp;
98     OscilRec Oscils[32];         /*there are 32 operators in YM2151*/
99     unsigned char Regs[256];     /*table of internal chip registers*/
100     unsigned char FeedBack[8];   /*feedback shift value for operators 0 in each channel*/
101     unsigned char ConnectTab[8]; /*connection algorithm number for each channel*/
102     unsigned char KC[8];        /*KC for each channel*/
103     unsigned char KF[32];       /*KF for each operator*/
104     unsigned char KS[32];       /*KS for each operator*/
105
106     unsigned char TimA, TimB, TimIRQ;
107     signed int TimAVal, TimBVal;
108     void * TimATimer, * TimBTimer;  /* ASG 980324 -- added for tracking timers */
109     void (* handler)(void);
110 } YM2151;
111
112
113 /*
114 ** Initialize YM2151 emulator(s).
115 **
116 ** 'num' is the number of virtual YM2151's to allocate
117 ** 'rate' is sampling rate and 'bufsiz' is the size of the
118 ** buffer that should be updated at each interval
119 */
120 int YMInit(int num, int clock, int rate, int sample_bits, int bufsiz, SAMPLE ** buffer);
121
122 /*
123 ** shutdown the YM2151 emulators ...
124 */
125 void YMShutdown(void);
126
127 /*
128 ** reset all chip registers for YM2151 number 'num'
129 */
130 void YMResetChip(int num);
131
132 /*
133 ** called to update all chips; should be called about 50 - 70 times per
134 ** second ... (depends on sample rate and buffer size)
135 */
136 void YM2151Update(void);
137
138 //void YM2151UpdateOne(int num, int endp);
139 void YM2151UpdateOne(void * BUF, int endp);
140
141 /*
142 ** write 'v' to register 'r' on YM2151 chip number 'n'
143 */
144 void YMWriteReg(int n, int r, int v);
145
146 /*
147 ** read status register on YM2151 chip number 'n'
148 */
149 unsigned char YMReadReg(unsigned char n);
150
151
152 /*
153 ** return pointer to synthesized data
154 */
155 SAMPLE * YMBuffer(int n);
156
157 void YMSetIrqHandler(int n, void (* handler)(void));
158
159 #endif /* _H_YM2151_ */
160