cs4231a.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * QEMU Crystal CS4231 audio chip emulation
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "audiodev.h"
  26. #include "audio/audio.h"
  27. #include "isa.h"
  28. #include "qdev.h"
  29. #include "qemu-timer.h"
  30. /*
  31. Missing features:
  32. ADC
  33. Loopback
  34. Timer
  35. ADPCM
  36. More...
  37. */
  38. /* #define DEBUG */
  39. /* #define DEBUG_XLAW */
  40. static struct {
  41. int aci_counter;
  42. } conf = {1};
  43. #ifdef DEBUG
  44. #define dolog(...) AUD_log ("cs4231a", __VA_ARGS__)
  45. #else
  46. #define dolog(...)
  47. #endif
  48. #define lwarn(...) AUD_log ("cs4231a", "warning: " __VA_ARGS__)
  49. #define lerr(...) AUD_log ("cs4231a", "error: " __VA_ARGS__)
  50. #define CS_REGS 16
  51. #define CS_DREGS 32
  52. typedef struct CSState {
  53. ISADevice dev;
  54. QEMUSoundCard card;
  55. qemu_irq pic;
  56. uint32_t regs[CS_REGS];
  57. uint8_t dregs[CS_DREGS];
  58. uint32_t irq;
  59. uint32_t dma;
  60. uint32_t port;
  61. int shift;
  62. int dma_running;
  63. int audio_free;
  64. int transferred;
  65. int aci_counter;
  66. SWVoiceOut *voice;
  67. int16_t *tab;
  68. } CSState;
  69. #define IO_READ_PROTO(name) \
  70. static uint32_t name (void *opaque, uint32_t addr)
  71. #define IO_WRITE_PROTO(name) \
  72. static void name (void *opaque, uint32_t addr, uint32_t val)
  73. #define GET_SADDR(addr) (addr & 3)
  74. #define MODE2 (1 << 6)
  75. #define MCE (1 << 6)
  76. #define PMCE (1 << 4)
  77. #define CMCE (1 << 5)
  78. #define TE (1 << 6)
  79. #define PEN (1 << 0)
  80. #define INT (1 << 0)
  81. #define IEN (1 << 1)
  82. #define PPIO (1 << 6)
  83. #define PI (1 << 4)
  84. #define CI (1 << 5)
  85. #define TI (1 << 6)
  86. enum {
  87. Index_Address,
  88. Index_Data,
  89. Status,
  90. PIO_Data
  91. };
  92. enum {
  93. Left_ADC_Input_Control,
  94. Right_ADC_Input_Control,
  95. Left_AUX1_Input_Control,
  96. Right_AUX1_Input_Control,
  97. Left_AUX2_Input_Control,
  98. Right_AUX2_Input_Control,
  99. Left_DAC_Output_Control,
  100. Right_DAC_Output_Control,
  101. FS_And_Playback_Data_Format,
  102. Interface_Configuration,
  103. Pin_Control,
  104. Error_Status_And_Initialization,
  105. MODE_And_ID,
  106. Loopback_Control,
  107. Playback_Upper_Base_Count,
  108. Playback_Lower_Base_Count,
  109. Alternate_Feature_Enable_I,
  110. Alternate_Feature_Enable_II,
  111. Left_Line_Input_Control,
  112. Right_Line_Input_Control,
  113. Timer_Low_Base,
  114. Timer_High_Base,
  115. RESERVED,
  116. Alternate_Feature_Enable_III,
  117. Alternate_Feature_Status,
  118. Version_Chip_ID,
  119. Mono_Input_And_Output_Control,
  120. RESERVED_2,
  121. Capture_Data_Format,
  122. RESERVED_3,
  123. Capture_Upper_Base_Count,
  124. Capture_Lower_Base_Count
  125. };
  126. static int freqs[2][8] = {
  127. { 8000, 16000, 27420, 32000, -1, -1, 48000, 9000 },
  128. { 5510, 11025, 18900, 22050, 37800, 44100, 33075, 6620 }
  129. };
  130. /* Tables courtesy http://hazelware.luggle.com/tutorials/mulawcompression.html */
  131. static int16_t MuLawDecompressTable[256] =
  132. {
  133. -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
  134. -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
  135. -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
  136. -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
  137. -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
  138. -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
  139. -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
  140. -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
  141. -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
  142. -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
  143. -876, -844, -812, -780, -748, -716, -684, -652,
  144. -620, -588, -556, -524, -492, -460, -428, -396,
  145. -372, -356, -340, -324, -308, -292, -276, -260,
  146. -244, -228, -212, -196, -180, -164, -148, -132,
  147. -120, -112, -104, -96, -88, -80, -72, -64,
  148. -56, -48, -40, -32, -24, -16, -8, 0,
  149. 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
  150. 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
  151. 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
  152. 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
  153. 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
  154. 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
  155. 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
  156. 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
  157. 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
  158. 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
  159. 876, 844, 812, 780, 748, 716, 684, 652,
  160. 620, 588, 556, 524, 492, 460, 428, 396,
  161. 372, 356, 340, 324, 308, 292, 276, 260,
  162. 244, 228, 212, 196, 180, 164, 148, 132,
  163. 120, 112, 104, 96, 88, 80, 72, 64,
  164. 56, 48, 40, 32, 24, 16, 8, 0
  165. };
  166. static int16_t ALawDecompressTable[256] =
  167. {
  168. -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
  169. -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
  170. -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
  171. -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
  172. -22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,
  173. -30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,
  174. -11008,-10496,-12032,-11520,-8960, -8448, -9984, -9472,
  175. -15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568,
  176. -344, -328, -376, -360, -280, -264, -312, -296,
  177. -472, -456, -504, -488, -408, -392, -440, -424,
  178. -88, -72, -120, -104, -24, -8, -56, -40,
  179. -216, -200, -248, -232, -152, -136, -184, -168,
  180. -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
  181. -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
  182. -688, -656, -752, -720, -560, -528, -624, -592,
  183. -944, -912, -1008, -976, -816, -784, -880, -848,
  184. 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
  185. 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
  186. 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
  187. 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
  188. 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
  189. 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
  190. 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
  191. 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
  192. 344, 328, 376, 360, 280, 264, 312, 296,
  193. 472, 456, 504, 488, 408, 392, 440, 424,
  194. 88, 72, 120, 104, 24, 8, 56, 40,
  195. 216, 200, 248, 232, 152, 136, 184, 168,
  196. 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
  197. 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
  198. 688, 656, 752, 720, 560, 528, 624, 592,
  199. 944, 912, 1008, 976, 816, 784, 880, 848
  200. };
  201. static void cs_reset (void *opaque)
  202. {
  203. CSState *s = opaque;
  204. s->regs[Index_Address] = 0x40;
  205. s->regs[Index_Data] = 0x00;
  206. s->regs[Status] = 0x00;
  207. s->regs[PIO_Data] = 0x00;
  208. s->dregs[Left_ADC_Input_Control] = 0x00;
  209. s->dregs[Right_ADC_Input_Control] = 0x00;
  210. s->dregs[Left_AUX1_Input_Control] = 0x88;
  211. s->dregs[Right_AUX1_Input_Control] = 0x88;
  212. s->dregs[Left_AUX2_Input_Control] = 0x88;
  213. s->dregs[Right_AUX2_Input_Control] = 0x88;
  214. s->dregs[Left_DAC_Output_Control] = 0x80;
  215. s->dregs[Right_DAC_Output_Control] = 0x80;
  216. s->dregs[FS_And_Playback_Data_Format] = 0x00;
  217. s->dregs[Interface_Configuration] = 0x08;
  218. s->dregs[Pin_Control] = 0x00;
  219. s->dregs[Error_Status_And_Initialization] = 0x00;
  220. s->dregs[MODE_And_ID] = 0x8a;
  221. s->dregs[Loopback_Control] = 0x00;
  222. s->dregs[Playback_Upper_Base_Count] = 0x00;
  223. s->dregs[Playback_Lower_Base_Count] = 0x00;
  224. s->dregs[Alternate_Feature_Enable_I] = 0x00;
  225. s->dregs[Alternate_Feature_Enable_II] = 0x00;
  226. s->dregs[Left_Line_Input_Control] = 0x88;
  227. s->dregs[Right_Line_Input_Control] = 0x88;
  228. s->dregs[Timer_Low_Base] = 0x00;
  229. s->dregs[Timer_High_Base] = 0x00;
  230. s->dregs[RESERVED] = 0x00;
  231. s->dregs[Alternate_Feature_Enable_III] = 0x00;
  232. s->dregs[Alternate_Feature_Status] = 0x00;
  233. s->dregs[Version_Chip_ID] = 0xa0;
  234. s->dregs[Mono_Input_And_Output_Control] = 0xa0;
  235. s->dregs[RESERVED_2] = 0x00;
  236. s->dregs[Capture_Data_Format] = 0x00;
  237. s->dregs[RESERVED_3] = 0x00;
  238. s->dregs[Capture_Upper_Base_Count] = 0x00;
  239. s->dregs[Capture_Lower_Base_Count] = 0x00;
  240. }
  241. static void cs_audio_callback (void *opaque, int free)
  242. {
  243. CSState *s = opaque;
  244. s->audio_free = free;
  245. }
  246. static void cs_reset_voices (CSState *s, uint32_t val)
  247. {
  248. int xtal;
  249. struct audsettings as;
  250. #ifdef DEBUG_XLAW
  251. if (val == 0 || val == 32)
  252. val = (1 << 4) | (1 << 5);
  253. #endif
  254. xtal = val & 1;
  255. as.freq = freqs[xtal][(val >> 1) & 7];
  256. if (as.freq == -1) {
  257. lerr ("unsupported frequency (val=%#x)\n", val);
  258. goto error;
  259. }
  260. as.nchannels = (val & (1 << 4)) ? 2 : 1;
  261. as.endianness = 0;
  262. s->tab = NULL;
  263. switch ((val >> 5) & ((s->dregs[MODE_And_ID] & MODE2) ? 7 : 3)) {
  264. case 0:
  265. as.fmt = AUD_FMT_U8;
  266. s->shift = as.nchannels == 2;
  267. break;
  268. case 1:
  269. s->tab = MuLawDecompressTable;
  270. goto x_law;
  271. case 3:
  272. s->tab = ALawDecompressTable;
  273. x_law:
  274. as.fmt = AUD_FMT_S16;
  275. as.endianness = AUDIO_HOST_ENDIANNESS;
  276. s->shift = as.nchannels == 2;
  277. break;
  278. case 6:
  279. as.endianness = 1;
  280. case 2:
  281. as.fmt = AUD_FMT_S16;
  282. s->shift = as.nchannels;
  283. break;
  284. case 7:
  285. case 4:
  286. lerr ("attempt to use reserved format value (%#x)\n", val);
  287. goto error;
  288. case 5:
  289. lerr ("ADPCM 4 bit IMA compatible format is not supported\n");
  290. goto error;
  291. }
  292. s->voice = AUD_open_out (
  293. &s->card,
  294. s->voice,
  295. "cs4231a",
  296. s,
  297. cs_audio_callback,
  298. &as
  299. );
  300. if (s->dregs[Interface_Configuration] & PEN) {
  301. if (!s->dma_running) {
  302. DMA_hold_DREQ (s->dma);
  303. AUD_set_active_out (s->voice, 1);
  304. s->transferred = 0;
  305. }
  306. s->dma_running = 1;
  307. }
  308. else {
  309. if (s->dma_running) {
  310. DMA_release_DREQ (s->dma);
  311. AUD_set_active_out (s->voice, 0);
  312. }
  313. s->dma_running = 0;
  314. }
  315. return;
  316. error:
  317. if (s->dma_running) {
  318. DMA_release_DREQ (s->dma);
  319. AUD_set_active_out (s->voice, 0);
  320. }
  321. }
  322. IO_READ_PROTO (cs_read)
  323. {
  324. CSState *s = opaque;
  325. uint32_t saddr, iaddr, ret;
  326. saddr = GET_SADDR (addr);
  327. iaddr = ~0U;
  328. switch (saddr) {
  329. case Index_Address:
  330. ret = s->regs[saddr] & ~0x80;
  331. break;
  332. case Index_Data:
  333. if (!(s->dregs[MODE_And_ID] & MODE2))
  334. iaddr = s->regs[Index_Address] & 0x0f;
  335. else
  336. iaddr = s->regs[Index_Address] & 0x1f;
  337. ret = s->dregs[iaddr];
  338. if (iaddr == Error_Status_And_Initialization) {
  339. /* keep SEAL happy */
  340. if (s->aci_counter) {
  341. ret |= 1 << 5;
  342. s->aci_counter -= 1;
  343. }
  344. }
  345. break;
  346. default:
  347. ret = s->regs[saddr];
  348. break;
  349. }
  350. dolog ("read %d:%d -> %d\n", saddr, iaddr, ret);
  351. return ret;
  352. }
  353. IO_WRITE_PROTO (cs_write)
  354. {
  355. CSState *s = opaque;
  356. uint32_t saddr, iaddr;
  357. saddr = GET_SADDR (addr);
  358. switch (saddr) {
  359. case Index_Address:
  360. if (!(s->regs[Index_Address] & MCE) && (val & MCE)
  361. && (s->dregs[Interface_Configuration] & (3 << 3)))
  362. s->aci_counter = conf.aci_counter;
  363. s->regs[Index_Address] = val & ~(1 << 7);
  364. break;
  365. case Index_Data:
  366. if (!(s->dregs[MODE_And_ID] & MODE2))
  367. iaddr = s->regs[Index_Address] & 0x0f;
  368. else
  369. iaddr = s->regs[Index_Address] & 0x1f;
  370. switch (iaddr) {
  371. case RESERVED:
  372. case RESERVED_2:
  373. case RESERVED_3:
  374. lwarn ("attempt to write %#x to reserved indirect register %d\n",
  375. val, iaddr);
  376. break;
  377. case FS_And_Playback_Data_Format:
  378. if (s->regs[Index_Address] & MCE) {
  379. cs_reset_voices (s, val);
  380. }
  381. else {
  382. if (s->dregs[Alternate_Feature_Status] & PMCE) {
  383. val = (val & ~0x0f) | (s->dregs[iaddr] & 0x0f);
  384. cs_reset_voices (s, val);
  385. }
  386. else {
  387. lwarn ("[P]MCE(%#x, %#x) is not set, val=%#x\n",
  388. s->regs[Index_Address],
  389. s->dregs[Alternate_Feature_Status],
  390. val);
  391. break;
  392. }
  393. }
  394. s->dregs[iaddr] = val;
  395. break;
  396. case Interface_Configuration:
  397. val &= ~(1 << 5); /* D5 is reserved */
  398. s->dregs[iaddr] = val;
  399. if (val & PPIO) {
  400. lwarn ("PIO is not supported (%#x)\n", val);
  401. break;
  402. }
  403. if (val & PEN) {
  404. if (!s->dma_running) {
  405. cs_reset_voices (s, s->dregs[FS_And_Playback_Data_Format]);
  406. }
  407. }
  408. else {
  409. if (s->dma_running) {
  410. DMA_release_DREQ (s->dma);
  411. AUD_set_active_out (s->voice, 0);
  412. s->dma_running = 0;
  413. }
  414. }
  415. break;
  416. case Error_Status_And_Initialization:
  417. lwarn ("attempt to write to read only register %d\n", iaddr);
  418. break;
  419. case MODE_And_ID:
  420. dolog ("val=%#x\n", val);
  421. if (val & MODE2)
  422. s->dregs[iaddr] |= MODE2;
  423. else
  424. s->dregs[iaddr] &= ~MODE2;
  425. break;
  426. case Alternate_Feature_Enable_I:
  427. if (val & TE)
  428. lerr ("timer is not yet supported\n");
  429. s->dregs[iaddr] = val;
  430. break;
  431. case Alternate_Feature_Status:
  432. if ((s->dregs[iaddr] & PI) && !(val & PI)) {
  433. /* XXX: TI CI */
  434. qemu_irq_lower (s->pic);
  435. s->regs[Status] &= ~INT;
  436. }
  437. s->dregs[iaddr] = val;
  438. break;
  439. case Version_Chip_ID:
  440. lwarn ("write to Version_Chip_ID register %#x\n", val);
  441. s->dregs[iaddr] = val;
  442. break;
  443. default:
  444. s->dregs[iaddr] = val;
  445. break;
  446. }
  447. dolog ("written value %#x to indirect register %d\n", val, iaddr);
  448. break;
  449. case Status:
  450. if (s->regs[Status] & INT) {
  451. qemu_irq_lower (s->pic);
  452. }
  453. s->regs[Status] &= ~INT;
  454. s->dregs[Alternate_Feature_Status] &= ~(PI | CI | TI);
  455. break;
  456. case PIO_Data:
  457. lwarn ("attempt to write value %#x to PIO register\n", val);
  458. break;
  459. }
  460. }
  461. static int cs_write_audio (CSState *s, int nchan, int dma_pos,
  462. int dma_len, int len)
  463. {
  464. int temp, net;
  465. uint8_t tmpbuf[4096];
  466. temp = len;
  467. net = 0;
  468. while (temp) {
  469. int left = dma_len - dma_pos;
  470. int copied;
  471. size_t to_copy;
  472. to_copy = audio_MIN (temp, left);
  473. if (to_copy > sizeof (tmpbuf)) {
  474. to_copy = sizeof (tmpbuf);
  475. }
  476. copied = DMA_read_memory (nchan, tmpbuf, dma_pos, to_copy);
  477. if (s->tab) {
  478. int i;
  479. int16_t linbuf[4096];
  480. for (i = 0; i < copied; ++i)
  481. linbuf[i] = s->tab[tmpbuf[i]];
  482. copied = AUD_write (s->voice, linbuf, copied << 1);
  483. copied >>= 1;
  484. }
  485. else {
  486. copied = AUD_write (s->voice, tmpbuf, copied);
  487. }
  488. temp -= copied;
  489. dma_pos = (dma_pos + copied) % dma_len;
  490. net += copied;
  491. if (!copied) {
  492. break;
  493. }
  494. }
  495. return net;
  496. }
  497. static int cs_dma_read (void *opaque, int nchan, int dma_pos, int dma_len)
  498. {
  499. CSState *s = opaque;
  500. int copy, written;
  501. int till = -1;
  502. copy = s->voice ? (s->audio_free >> (s->tab != NULL)) : dma_len;
  503. if (s->dregs[Pin_Control] & IEN) {
  504. till = (s->dregs[Playback_Lower_Base_Count]
  505. | (s->dregs[Playback_Upper_Base_Count] << 8)) << s->shift;
  506. till -= s->transferred;
  507. copy = audio_MIN (till, copy);
  508. }
  509. if ((copy <= 0) || (dma_len <= 0)) {
  510. return dma_pos;
  511. }
  512. written = cs_write_audio (s, nchan, dma_pos, dma_len, copy);
  513. dma_pos = (dma_pos + written) % dma_len;
  514. s->audio_free -= (written << (s->tab != NULL));
  515. if (written == till) {
  516. s->regs[Status] |= INT;
  517. s->dregs[Alternate_Feature_Status] |= PI;
  518. s->transferred = 0;
  519. qemu_irq_raise (s->pic);
  520. }
  521. else {
  522. s->transferred += written;
  523. }
  524. return dma_pos;
  525. }
  526. static int cs4231a_pre_load (void *opaque)
  527. {
  528. CSState *s = opaque;
  529. if (s->dma_running) {
  530. DMA_release_DREQ (s->dma);
  531. AUD_set_active_out (s->voice, 0);
  532. }
  533. s->dma_running = 0;
  534. return 0;
  535. }
  536. static int cs4231a_post_load (void *opaque, int version_id)
  537. {
  538. CSState *s = opaque;
  539. if (s->dma_running && (s->dregs[Interface_Configuration] & PEN)) {
  540. s->dma_running = 0;
  541. cs_reset_voices (s, s->dregs[FS_And_Playback_Data_Format]);
  542. }
  543. return 0;
  544. }
  545. static const VMStateDescription vmstate_cs4231a = {
  546. .name = "cs4231a",
  547. .version_id = 1,
  548. .minimum_version_id = 1,
  549. .minimum_version_id_old = 1,
  550. .pre_load = cs4231a_pre_load,
  551. .post_load = cs4231a_post_load,
  552. .fields = (VMStateField []) {
  553. VMSTATE_UINT32_ARRAY(regs, CSState, CS_REGS),
  554. VMSTATE_BUFFER(dregs, CSState),
  555. VMSTATE_INT32(dma_running, CSState),
  556. VMSTATE_INT32(audio_free, CSState),
  557. VMSTATE_INT32(transferred, CSState),
  558. VMSTATE_INT32(aci_counter, CSState),
  559. VMSTATE_END_OF_LIST()
  560. }
  561. };
  562. static int cs4231a_initfn (ISADevice *dev)
  563. {
  564. CSState *s = DO_UPCAST (CSState, dev, dev);
  565. int i;
  566. isa_init_irq (dev, &s->pic, s->irq);
  567. for (i = 0; i < 4; i++) {
  568. isa_init_ioport(dev, i);
  569. register_ioport_write (s->port + i, 1, 1, cs_write, s);
  570. register_ioport_read (s->port + i, 1, 1, cs_read, s);
  571. }
  572. DMA_register_channel (s->dma, cs_dma_read, s);
  573. qemu_register_reset (cs_reset, s);
  574. cs_reset (s);
  575. AUD_register_card ("cs4231a", &s->card);
  576. return 0;
  577. }
  578. int cs4231a_init (qemu_irq *pic)
  579. {
  580. isa_create_simple ("cs4231a");
  581. return 0;
  582. }
  583. static ISADeviceInfo cs4231a_info = {
  584. .qdev.name = "cs4231a",
  585. .qdev.desc = "Crystal Semiconductor CS4231A",
  586. .qdev.size = sizeof (CSState),
  587. .qdev.vmsd = &vmstate_cs4231a,
  588. .init = cs4231a_initfn,
  589. .qdev.props = (Property[]) {
  590. DEFINE_PROP_HEX32 ("iobase", CSState, port, 0x534),
  591. DEFINE_PROP_UINT32 ("irq", CSState, irq, 9),
  592. DEFINE_PROP_UINT32 ("dma", CSState, dma, 3),
  593. DEFINE_PROP_END_OF_LIST (),
  594. },
  595. };
  596. static void cs4231a_register (void)
  597. {
  598. isa_qdev_register (&cs4231a_info);
  599. }
  600. device_init (cs4231a_register)