2
0

screamer.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /*
  2. * File: Screamer.c
  3. * Description: Implement the Screamer sound chip used in Apple Macintoshes.
  4. * It works by filling a buffer, then playing the buffer.
  5. */
  6. #include "qemu/osdep.h"
  7. #include "audio/audio.h"
  8. #include "hw/hw.h"
  9. #include "hw/irq.h"
  10. #include <inttypes.h>
  11. #include "hw/ppc/mac_dbdma.h"
  12. #include "hw/qdev-properties.h"
  13. #include "migration/vmstate.h"
  14. #include "include/hw/audio/screamer.h"
  15. #define DEBUG_SCREAMER 0
  16. #define DPRINTF(fmt, ...) \
  17. do { if (DEBUG_SCREAMER) { printf(fmt , ## __VA_ARGS__); } } while (0)
  18. #define SOUND_CONTROL_REG 0
  19. #define CODEC_CONTROL_REG 1
  20. #define CODEC_STATUS_REG 2
  21. #define CLIP_COUNT_REG 3
  22. #define BYTE_SWAP_REG 4
  23. #define FRAME_COUNT_REG 5
  24. #define AWACS_BUSY 0x01000000
  25. /* Used with AWACS register 1 */
  26. #define RECALIBRATE 0x004
  27. #define LOOPTHRU 0x040
  28. #define SPEAKER_MUTE 0x080
  29. #define HEADPHONE_MUTE 0x200
  30. #define OUTPUT_ZERO 0x400
  31. #define OUTPUT_ONE 0x800
  32. #define PARALLEL_OUTPUT 0xc00
  33. /* Function prototypes */
  34. static uint32_t set_busy_bit(uint32_t value, int bit);
  35. static uint32_t set_part_ready_bit(uint32_t value, int bit_value);
  36. static uint32_t set_revision(uint32_t input_value);
  37. static uint32_t set_manufacturer(uint32_t input_value);
  38. static int get_sampling_rate(ScreamerState *s);
  39. static uint32_t get_frame_count_reg(ScreamerState *s);
  40. static void add_to_speaker_buffer(DBDMA_io *io);
  41. static void dma_request(DBDMA_io *io);
  42. /**************************** Getters *************************/
  43. /* Returns the codec control register's encoded AWACS address */
  44. static uint8_t get_codec_control_address(uint32_t value)
  45. {
  46. uint8_t return_value;
  47. return_value = (value >> 12) & 0x00000fff;
  48. return return_value;
  49. }
  50. static uint32_t get_sound_control_reg(ScreamerState *s)
  51. {
  52. DPRINTF("%s() called - returned 0x%x\n", __func__, s->sound_control);
  53. return s->sound_control;
  54. }
  55. /* The AWACS registers are accessed thru this register */
  56. static uint32_t get_codec_control_reg(ScreamerState *s)
  57. {
  58. int awacs_register = get_codec_control_address(s->codec_control);
  59. uint32_t return_value = s->awacs[awacs_register];
  60. return_value = set_busy_bit(return_value, 0); /* Tell CPU we are ready */
  61. DPRINTF("%s() called - returned 0x%x\tAWACS register: %d\n", __func__,
  62. return_value, awacs_register);
  63. return return_value;
  64. }
  65. /*
  66. * Determines if the readback bit is set.
  67. * It is used by the Codec Control register.
  68. */
  69. static bool readback_enabled(ScreamerState *s)
  70. {
  71. /* Note: bit zero is the readback enabled bit */
  72. if (s->awacs[7] & 1) {
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }
  78. static uint32_t get_codec_status_reg(ScreamerState *s)
  79. {
  80. uint32_t return_value;
  81. /* if in readback mode - return AWACS register value */
  82. if (readback_enabled(s)) {
  83. int awacs_register = (s->awacs[7] & 0xe) >> 1;
  84. s->awacs[7] = s->awacs[7] & 0xfffffffe; /* turn off readback mode */
  85. return_value = s->awacs[awacs_register] << 4;
  86. DPRINTF("readback enable bit is set, returning AWACS register %d\t"
  87. "value:0x%x\n", awacs_register, return_value);
  88. return return_value;
  89. }
  90. /* Tell CPU we are ready */
  91. return_value = set_part_ready_bit(s->codec_status, 1);
  92. /* Set Revision to Screamer */
  93. return_value = set_revision(return_value);
  94. /* Set the Manufacturer to Crystal */
  95. return_value = set_manufacturer(return_value);
  96. DPRINTF("%s() called - returned 0x%x\n", __func__, return_value);
  97. return return_value;
  98. }
  99. static uint32_t get_clip_count_reg(ScreamerState *s)
  100. {
  101. DPRINTF("%s() called - returned 0x%x\n", __func__, s->clip_count);
  102. uint32_t return_value;
  103. return_value = s->clip_count;
  104. /* This is reset everytime it is read */
  105. s->clip_count = 0;
  106. return return_value;
  107. }
  108. static uint32_t get_byte_swap_reg(ScreamerState *s)
  109. {
  110. DPRINTF("%s() called - returned 0x%x\n", __func__, s->byte_swap);
  111. /*
  112. * If all you hear is noise, it could be this register reporting the
  113. * wrong value.
  114. */
  115. return s->byte_swap ? 0 : 1;
  116. }
  117. /*
  118. * Returns the frame (sample) count
  119. */
  120. static uint32_t get_frame_count_reg(ScreamerState *s)
  121. {
  122. DPRINTF("%s() called - returned 0x%x\n", __func__, s->frame_count);
  123. return s->frame_count;
  124. }
  125. static uint8_t get_left_vol(uint32_t value)
  126. {
  127. return value & 0xf;
  128. }
  129. static uint8_t get_right_vol(uint32_t value)
  130. {
  131. return value & 0x3c0 >> 6;
  132. }
  133. /*
  134. * Returns the sampling rate.
  135. * If the audio is playing back too fast or too slow, this function may be the
  136. * cause.
  137. */
  138. static int get_sampling_rate(ScreamerState *s)
  139. {
  140. uint32_t screamer_rate = s->sound_control & 0x700;
  141. int return_value;
  142. /* All return values are in Hertz */
  143. switch (screamer_rate) {
  144. case 0x0:
  145. return_value = 44100;
  146. break;
  147. case 0x100:
  148. return_value = 29400;
  149. break;
  150. case 0x200:
  151. return_value = 22050;
  152. break;
  153. case 0x300:
  154. return_value = 17640;
  155. break;
  156. case 0x400:
  157. return_value = 14700;
  158. break;
  159. case 0x500:
  160. return_value = 11025;
  161. break;
  162. case 0x600:
  163. return_value = 8820;
  164. break;
  165. case 0x700:
  166. return_value = 7350;
  167. break;
  168. default:
  169. DPRINTF("get_sampling_rate() unknown value: 0x%x\nDefaulting to"
  170. " 44100 Hz.\n", screamer_rate);
  171. return 44100;
  172. }
  173. DPRINTF("%s() called - returning %dHz\n", __func__, return_value);
  174. return return_value;
  175. }
  176. /**************************** End of getters *************************/
  177. /***************************** Speaker call back *************************/
  178. /* resets the play and buffer position markers */
  179. static void reset_markers(ScreamerState *s)
  180. {
  181. s->spk_play_position = 0;
  182. s->spk_buffer_position = 0;
  183. }
  184. /* Sends the samples to the host for playing */
  185. static void send_samples_to_host(ScreamerState *s, int max_samples)
  186. {
  187. int write_length, requested_length;
  188. requested_length = MIN(max_samples, (s->spk_buffer_position -
  189. s->spk_play_position));
  190. write_length = AUD_write(s->speaker_voice,
  191. &s->spk_buffer[s->spk_play_position],
  192. requested_length);
  193. DPRINTF("requested length: %d\twrite length: %d\t",
  194. requested_length, write_length);
  195. s->spk_play_position += write_length;
  196. DPRINTF("AUD_write %d/%d\n", s->spk_play_position, s->spk_buffer_position);
  197. s->frame_count += write_length;
  198. }
  199. /*
  200. * Called by QEMU's audio system to tell the output backend to send samples
  201. * from the buffer to the host sound system.
  202. * opaque: a pointer to the ScreamerState instance.
  203. * max_samples: the number of samples that can be sent to the hardware buffer.
  204. */
  205. static void speaker_callback(void *opaque, int max_samples)
  206. {
  207. ScreamerState *s = (ScreamerState *) opaque;
  208. /* if we have more samples to play */
  209. if (s->spk_buffer_position > 0) {
  210. if (s->spk_buffer_position > s->spk_play_position) {
  211. DPRINTF("%s() called - max_samples: %d\n", __func__, max_samples);
  212. send_samples_to_host(s, max_samples);
  213. }
  214. if (s->spk_play_position >= s->spk_buffer_position) {
  215. DPRINTF("done playing buffer\n");
  216. DPRINTF("pp: %d\tbp: %d\n", s->spk_play_position,
  217. s->spk_buffer_position);
  218. if (s->spk_play_position > s->spk_buffer_position) {
  219. DPRINTF("Error detected! - pp > bp\n\a");
  220. }
  221. reset_markers(s);
  222. /* play postponed samples */
  223. if (s->dma_io.len > 0) {
  224. DPRINTF("playing postponed samples\n");
  225. add_to_speaker_buffer(&s->dma_io);
  226. return;
  227. }
  228. }
  229. }
  230. }
  231. /************************* End of speaker call back *************************/
  232. /* Opens the speaker's voice */
  233. static void open_speaker_voice(ScreamerState *s)
  234. {
  235. DPRINTF("%s() called\n", __func__);
  236. /* if voice is already open return from function */
  237. if (s->speaker_voice != NULL) {
  238. DPRINTF("closing speaker voice\n");
  239. AUD_close_out(&s->card, s->speaker_voice);
  240. s->speaker_voice = NULL;
  241. }
  242. struct audsettings audio_settings;
  243. audio_settings.freq = get_sampling_rate(s); /* in hertz */
  244. audio_settings.nchannels = 2; /* stereo output */
  245. audio_settings.fmt = AUDIO_FORMAT_S16; /* signed 16 bit */
  246. audio_settings.endianness = get_byte_swap_reg(s); /* endianness */
  247. s->speaker_voice = AUD_open_out(&s->card, s->speaker_voice, SOUND_CHIP_NAME
  248. " speaker", s, speaker_callback,
  249. &audio_settings);
  250. if (!s->speaker_voice) {
  251. AUD_log(SOUND_CHIP_NAME, "Out voice could not be opened\n");
  252. } else {
  253. AUD_set_active_out(s->speaker_voice, true);
  254. }
  255. }
  256. /******************************* Setters *************************************/
  257. /* Updates QEMU's audio backend settings */
  258. static void set_QEMU_audio_settings(ScreamerState *s)
  259. {
  260. DPRINTF("%s() called\n", __func__);
  261. open_speaker_voice(s);
  262. }
  263. /* Return value: 1 = muted 0 = not muted */
  264. static int is_muted(ScreamerState *s)
  265. {
  266. int mute_state = s->awacs[1] & SPEAKER_MUTE ? 1 : 0;
  267. if (s->awacs[1] & SPEAKER_MUTE) {
  268. DPRINTF("speaker is muted\n");
  269. } else {
  270. DPRINTF("speaker is unmuted\n");
  271. }
  272. if (s->awacs[1] & HEADPHONE_MUTE) {
  273. DPRINTF("headphone is muted\n");
  274. } else {
  275. DPRINTF("headphone is unmuted\n");
  276. }
  277. return mute_state;
  278. }
  279. /* Converts Screamer's volume system to QEMU's system */
  280. static int screamer_to_qemu_volume(int x)
  281. {
  282. return -16 * x + 240;
  283. }
  284. /* Sets QEMU's volume. */
  285. static void set_volume(ScreamerState *s)
  286. {
  287. int should_mute = is_muted(s);
  288. /* Get Screamer volume values */
  289. uint8_t left_vol = get_left_vol(s->awacs[4]);
  290. uint8_t right_vol = get_right_vol(s->awacs[4]);
  291. DPRINTF("set_volume() called - M:%d\tL:%d\tR:%d\n", should_mute, left_vol,
  292. right_vol);
  293. /* Convert Screamer to QEMU volume values */
  294. left_vol = screamer_to_qemu_volume(left_vol);
  295. right_vol = screamer_to_qemu_volume(right_vol);
  296. DPRINTF("QEMU volume: L:%d\tR:%d\n", left_vol, right_vol);
  297. AUD_set_volume_out(s->speaker_voice, should_mute, left_vol, right_vol);
  298. }
  299. /* Sets the sound control register */
  300. static void set_sound_control_reg(ScreamerState *s, uint32_t value)
  301. {
  302. DPRINTF("set_sound_control_reg() called - value: 0x%x\n", value);
  303. s->sound_control = value;
  304. set_QEMU_audio_settings(s);
  305. }
  306. /* Used for input gain only - can be ignored for now. */
  307. static void set_awacs_0_reg(ScreamerState *s, uint32_t new_value)
  308. {
  309. DPRINTF("Settings AWACS register 0 to 0x%x\n", s->awacs[0]);
  310. s->awacs[0] = new_value;
  311. }
  312. static void set_awacs_1_reg(ScreamerState *s, uint32_t new_value)
  313. {
  314. DPRINTF("Settings AWACS register 1 to 0x%x\n", new_value);
  315. s->awacs[1] = new_value;
  316. /* If recalibration requested */
  317. if (new_value & RECALIBRATE) {
  318. DPRINTF("Recalibration requested - unimplemented\n");
  319. new_value = new_value ^ RECALIBRATE; /* Turn off recalibrate bit */
  320. }
  321. /* If loop thru set - what does this mean? */
  322. if (new_value & LOOPTHRU) {
  323. DPRINTF("Loopthru enabled - doing nothing\n");
  324. }
  325. /* Set headphone jack mute state */
  326. if (new_value & HEADPHONE_MUTE) {
  327. DPRINTF("Headphone muted\n");
  328. }
  329. else {
  330. DPRINTF("Headphone unmuted\n");
  331. }
  332. if (new_value & SPEAKER_MUTE) {
  333. DPRINTF("Speaker muted\n");
  334. }
  335. else {
  336. DPRINTF("Speaker unmuted\n");
  337. }
  338. if (new_value & OUTPUT_ZERO) {
  339. DPRINTF("output zero set - not sure what this means\n");
  340. }
  341. if (new_value & OUTPUT_ONE) {
  342. DPRINTF("output one set - not sure what this means\n");
  343. }
  344. if (new_value & PARALLEL_OUTPUT) {
  345. DPRINTF("parallel port enabled - but no parallel port here\n");
  346. }
  347. set_volume(s);
  348. }
  349. /* This is used for headphone volume - not needed */
  350. static void set_awacs_2_reg(ScreamerState *s, uint32_t new_value)
  351. {
  352. DPRINTF("Settings AWACS register 2 to 0x%x\n"
  353. "Ignoring change in headphone volume.\n", s->awacs[2]);
  354. s->awacs[2] = new_value;
  355. }
  356. /* Unknown register purpose */
  357. static void set_awacs_3_reg(ScreamerState *s, uint32_t new_value)
  358. {
  359. DPRINTF("Settings AWACS register 3 to 0x%x\n"
  360. "This register has an unknown purpose and does not do anything\n",
  361. s->awacs[3]);
  362. s->awacs[3] = new_value;
  363. }
  364. /* Mostly deals with speaker volume */
  365. static void set_awacs_4_reg(ScreamerState *s, uint32_t new_value)
  366. {
  367. DPRINTF("AWACS register 4 write: 0x%x\n", new_value);
  368. s->awacs[4] = new_value;
  369. set_volume(s);
  370. }
  371. /* This register is about loop thru stuff I don't understand */
  372. static void set_awacs_5_reg(ScreamerState *s, uint32_t new_value)
  373. {
  374. DPRINTF("Settings AWACS register 5 to 0x%x\n"
  375. "Loop thru update ignored.\n", s->awacs[5]);
  376. s->awacs[5] = new_value;
  377. }
  378. /* Prints the states of the AWACS power register */
  379. static void print_power_reg_values(uint32_t value)
  380. {
  381. if ((value & 0x3) == 0) {
  382. printf("Screamer run state set\n");
  383. }
  384. if ((value & 0x3) == 1) {
  385. printf("Screamer doze state set\n");
  386. }
  387. if ((value & 0x3) == 2) {
  388. printf("Screamer idle state set\n");
  389. }
  390. }
  391. /* Power Magement register */
  392. static void set_awacs_6_reg(ScreamerState *s, uint32_t new_value)
  393. {
  394. DPRINTF("Settings AWACS register 6 to 0x%x\n"
  395. "Power management update ignored.\n", s->awacs[6]);
  396. if (DEBUG_SCREAMER) {
  397. print_power_reg_values(new_value);
  398. }
  399. s->awacs[6] = new_value;
  400. }
  401. /* Read Back - repeating something that was sent to this chip? */
  402. static void set_awacs_7_reg(ScreamerState *s, uint32_t new_value)
  403. {
  404. DPRINTF("Settings AWACS register 7 to 0x%x\n", new_value);
  405. s->awacs[7] = new_value;
  406. }
  407. /* Sets the AWACs registers - a.k.a. shadow registers */
  408. static void set_awacs_register(ScreamerState *s, uint32_t value)
  409. {
  410. int the_register = get_codec_control_address(value);
  411. switch (the_register) {
  412. case 0:
  413. set_awacs_0_reg(s, value);
  414. break;
  415. case 1:
  416. set_awacs_1_reg(s, value);
  417. break;
  418. case 2:
  419. set_awacs_2_reg(s, value);
  420. break;
  421. case 3:
  422. set_awacs_3_reg(s, value);
  423. break;
  424. case 4:
  425. set_awacs_4_reg(s, value);
  426. break;
  427. case 5:
  428. set_awacs_5_reg(s, value);
  429. break;
  430. case 6:
  431. set_awacs_6_reg(s, value);
  432. break;
  433. case 7:
  434. set_awacs_7_reg(s, value);
  435. break;
  436. default:
  437. DPRINTF("Unhandled awacs registers %d\n", the_register);
  438. }
  439. }
  440. /* Used to set the AWACS registers */
  441. static void set_codec_control_reg(ScreamerState *s, uint32_t value)
  442. {
  443. DPRINTF("set_codec_control_reg() called - value: 0x%x\n", value);
  444. s->codec_control = value;
  445. set_awacs_register(s, value);
  446. }
  447. static void set_codec_status_reg(ScreamerState *s, uint32_t value)
  448. {
  449. DPRINTF("set_codec_status_reg() called - value: 0x%x\n", value);
  450. s->codec_status = value;
  451. }
  452. static void set_clip_count_reg(ScreamerState *s, uint32_t new_value)
  453. {
  454. DPRINTF("set_clip_count_reg() called - value: 0x%x\n", new_value);
  455. s->clip_count = new_value;
  456. }
  457. static void set_byte_swap_reg(ScreamerState *s, uint32_t value)
  458. {
  459. DPRINTF("set_byte_swap_reg() called - value: 0x%x\n", value);
  460. s->byte_swap = value;
  461. }
  462. static void set_frame_count_reg(ScreamerState *s, uint32_t new_value)
  463. {
  464. DPRINTF("%s() called - value: 0x%x\n", __func__, new_value);
  465. s->frame_count = new_value;
  466. }
  467. /*
  468. * Sets the busy bit of codec control register.
  469. * It is used to tell the CPU to wait.
  470. * value: the codec control register's value
  471. * bit_value: used to set or disable the busy bit
  472. */
  473. static uint32_t set_busy_bit(uint32_t value, int bit_value)
  474. {
  475. const int busy_bit = 0x01000000;
  476. uint32_t return_value;
  477. if (bit_value == 1) /* Set this bit */
  478. return_value = (value | busy_bit);
  479. else /* bit_value == 0 Disable this bit */
  480. return_value = (value & ~busy_bit);
  481. return return_value;
  482. }
  483. /*
  484. * Sets the part ready bit of the codec status register
  485. * value: the codec status register's value
  486. * bit_value: used to set or disable the part ready bit
  487. */
  488. static uint32_t set_part_ready_bit(uint32_t value, int bit_value)
  489. {
  490. const int part_ready_bit = 0x00400000;
  491. uint32_t return_value;
  492. if (bit_value == 1) /* Set this bit */
  493. return_value = (value | part_ready_bit);
  494. else /* bit_value == 0 Disable this bit */
  495. return_value = (value & ~part_ready_bit);
  496. return return_value;
  497. }
  498. /* Sets bits 12 and 13 to 1 to indicate the Screamer revision */
  499. static uint32_t set_revision(uint32_t input_value)
  500. {
  501. uint32_t return_value;
  502. return_value = input_value | 0x3000;
  503. return return_value;
  504. }
  505. /* Sets bit 8 to indicate Crystal as the manufacturer */
  506. static uint32_t set_manufacturer(uint32_t input_value)
  507. {
  508. uint32_t return_value;
  509. return_value = input_value | 0x100;
  510. return return_value;
  511. }
  512. /************************** End of Setters *********************************/
  513. /*************************** DMA functions *********************************/
  514. /*
  515. * Sends audio samples from a microphone or line-in to memory.
  516. * Used for sound input.
  517. * Currently only prevents a deadlock condition with Mac OS 9.
  518. */
  519. static void screamer_to_dma(DBDMA_io *io)
  520. {
  521. DPRINTF("%s() called\n", __func__);
  522. ScreamerState *s = (ScreamerState *)io->opaque;
  523. DBDMAState *dbs = s->dbdma;
  524. DBDMA_channel *ch = &dbs->channels[0x12];
  525. ch->regs[DBDMA_STATUS] |= DEAD;
  526. ch->regs[DBDMA_STATUS] &= ~ACTIVE;
  527. io->dma_end(io);
  528. return;
  529. }
  530. static void print_dma_info(DBDMA_io *io)
  531. {
  532. #define RUN 0x8000
  533. #define PAUSE 0x4000
  534. #define FLUSH 0x2000
  535. #define WAKE 0x1000
  536. #define DEAD 0x0800
  537. #define ACTIVE 0x0400
  538. #define BT 0x0100
  539. #define DEVSTAT 0x00ff
  540. /*
  541. * RUN and PAUSE are bits under software control only.
  542. * FLUSH and WAKE are set by SW and cleared by hardware.
  543. * DEAD, ACTIVE and BT are only under hardware control.
  544. */
  545. DBDMA_channel *ch = io->channel;
  546. printf("DMA FLAGS: ");
  547. if (ch->regs[DBDMA_STATUS] & RUN) {
  548. printf("RUN ");
  549. }
  550. if (ch->regs[DBDMA_STATUS] & ACTIVE) {
  551. printf("ACTIVE ");
  552. }
  553. if (ch->regs[DBDMA_STATUS] & PAUSE) {
  554. printf("PAUSE ");
  555. }
  556. if (ch->regs[DBDMA_STATUS] & DEAD) {
  557. printf("DEAD ");
  558. }
  559. if (ch->regs[DBDMA_STATUS] & WAKE) {
  560. printf("WAKE ");
  561. }
  562. if (ch->regs[DBDMA_STATUS] & BT) {
  563. printf("BT ");
  564. }
  565. if (ch->regs[DBDMA_STATUS] & DEVSTAT) {
  566. printf("DEVSTAT ");
  567. }
  568. if (ch->regs[DBDMA_STATUS] & FLUSH) {
  569. printf("FLUSH ");
  570. }
  571. if (ch->io.processing == true) {
  572. printf("processing ");
  573. }
  574. printf("\n");
  575. }
  576. /* Tell the DMA controller we request more samples */
  577. static void dma_request(DBDMA_io *io)
  578. {
  579. DPRINTF("%s() called\n", __func__);
  580. if (DEBUG_SCREAMER) {
  581. print_dma_info(io);
  582. }
  583. io->len = 0;
  584. io->dma_end(io);
  585. }
  586. /* Adds sample data to the buffer */
  587. static void add_to_speaker_buffer(DBDMA_io *io)
  588. {
  589. ScreamerState *s = (ScreamerState *) io->opaque;
  590. if (s->spk_buffer_position + io->len > MAX_BUFFER_SIZE) {
  591. /* postpone calling these samples until the buffer has been emptied */
  592. memcpy(&s->dma_io, io, sizeof(DBDMA_io));
  593. return;
  594. }
  595. dma_memory_read(&address_space_memory, io->addr,
  596. &s->spk_buffer[s->spk_buffer_position], io->len,
  597. MEMTXATTRS_UNSPECIFIED);
  598. s->spk_buffer_position += io->len;
  599. DPRINTF("%s() called - len: %d pos: %d/%d\n", __func__, io->len,
  600. s->spk_buffer_position, MAX_BUFFER_SIZE);
  601. dma_request(io);
  602. }
  603. /*
  604. * Called by the DMA chip to transfer samples from memory to the
  605. * Screamer chip.
  606. * Used for sound output.
  607. */
  608. static void dma_to_screamer(DBDMA_io *io)
  609. {
  610. add_to_speaker_buffer(io);
  611. }
  612. /*
  613. * This will flush the audio buffer of previous audio - eliminating previous
  614. * audio playback.
  615. */
  616. static void send_silence_to_speaker(ScreamerState *s)
  617. {
  618. DPRINTF("Silencing audio buffer...\n");
  619. int length = MAX_BUFFER_SIZE;
  620. s->spk_buffer_position = length;
  621. s->spk_play_position = 0;
  622. memset(s->spk_buffer, 0, length);
  623. s->dma_io.len = 0; /* stop any postponed samples from playing */
  624. }
  625. /* This is called after audio stops playing */
  626. static void dma_send_flush(DBDMA_io *io)
  627. {
  628. DPRINTF("dma_send_flush() called\n");
  629. if (DEBUG_SCREAMER) {
  630. print_dma_info(io);
  631. }
  632. ScreamerState *s = (ScreamerState *)io->opaque;
  633. reset_markers(s);
  634. send_silence_to_speaker(s);
  635. if (io->len > 0) {
  636. dma_request(io);
  637. }
  638. }
  639. static void dma_receive_flush(DBDMA_io *io)
  640. {
  641. DPRINTF("dma_receive_flush() called\n");
  642. }
  643. /* Set the functions the DMA system will call */
  644. void screamer_register_dma_functions(ScreamerState *s, void *dbdma,
  645. int send_channel, int receive_channel)
  646. {
  647. DPRINTF("%s() called\n", __func__);
  648. DPRINTF("send channel: %d\treceive channel: %d\n", send_channel,
  649. receive_channel);
  650. s->dbdma = dbdma;
  651. /* Setup the DMA send system */
  652. DBDMA_register_channel(s->dbdma, send_channel, s->dma_send_irq,
  653. dma_to_screamer, dma_send_flush, s);
  654. /* Setup the DMA receive system */
  655. DBDMA_register_channel(s->dbdma, receive_channel, s->dma_receive_irq,
  656. screamer_to_dma, dma_receive_flush, s);
  657. }
  658. /************************* End of DMA functions **************************/
  659. /* Resets this sound chip */
  660. static void screamer_reset(DeviceState *d)
  661. {
  662. DPRINTF("screamer_reset() called\n");
  663. ScreamerState *s = SCREAMER(d);
  664. set_sound_control_reg(s, 0);
  665. set_codec_control_reg(s, 0);
  666. set_codec_status_reg(s, 0);
  667. set_clip_count_reg(s, 0);
  668. set_byte_swap_reg(s, 0);
  669. set_frame_count_reg(s, 0);
  670. int i, num_awacs_regs = 8;
  671. for (i = 0; i < num_awacs_regs; i++) {
  672. s->awacs[i] = 0;
  673. }
  674. set_QEMU_audio_settings(s);
  675. reset_markers(s);
  676. s->dma_io.len = 0;
  677. }
  678. /* Called when the CPU reads the memory addresses assigned to Screamer */
  679. static uint64_t screamer_mmio_read(void *opaque, hwaddr addr, unsigned size)
  680. {
  681. ScreamerState *state = opaque;
  682. uint32_t return_value;
  683. addr = addr >> 4;
  684. switch (addr) {
  685. case SOUND_CONTROL_REG:
  686. return_value = get_sound_control_reg(state);
  687. break;
  688. case CODEC_CONTROL_REG:
  689. return_value = get_codec_control_reg(state);
  690. break;
  691. case CODEC_STATUS_REG:
  692. return_value = get_codec_status_reg(state);
  693. break;
  694. case CLIP_COUNT_REG:
  695. return_value = get_clip_count_reg(state);
  696. break;
  697. case BYTE_SWAP_REG:
  698. return_value = get_byte_swap_reg(state);
  699. break;
  700. case FRAME_COUNT_REG:
  701. return_value = get_frame_count_reg(state);
  702. break;
  703. default:
  704. DPRINTF("Unknown register read - addr:%" HWADDR_PRIx "\tsize:%d\n",
  705. addr, size);
  706. return_value = 12021981; /* Value used for debugging purposes */
  707. }
  708. DPRINTF("screamer_mmio_read() called addr: %" HWADDR_PRIx " size: %d",
  709. addr >> 4, size);
  710. DPRINTF(" returning 0x%x\n", return_value);
  711. return return_value;
  712. }
  713. /* Called when the CPU writes to the memory addresses assigned to Screamer */
  714. static void screamer_mmio_write(void *opaque, hwaddr addr, uint64_t raw_value,
  715. unsigned size)
  716. {
  717. DPRINTF("screamer_mmio_write() called - size: %d\n", size);
  718. ScreamerState *state = opaque;
  719. uint32_t value = raw_value & 0xffffffff;
  720. addr = addr >> 4;
  721. switch (addr) {
  722. case SOUND_CONTROL_REG:
  723. set_sound_control_reg(state, value);
  724. break;
  725. case CODEC_CONTROL_REG:
  726. set_codec_control_reg(state, value);
  727. break;
  728. case CODEC_STATUS_REG:
  729. set_codec_status_reg(state, value);
  730. break;
  731. case CLIP_COUNT_REG:
  732. set_clip_count_reg(state, value);
  733. break;
  734. case BYTE_SWAP_REG:
  735. set_byte_swap_reg(state, value);
  736. break;
  737. case FRAME_COUNT_REG:
  738. set_frame_count_reg(state, value);
  739. break;
  740. default:
  741. DPRINTF("Unknown register write - addr:%" HWADDR_PRIx "\tvalue:%d\n",
  742. addr, value);
  743. }
  744. }
  745. /* Used for memory_region_init_io() for memory mapped I/O */
  746. static const MemoryRegionOps screamer_ops = {
  747. .read = screamer_mmio_read,
  748. .write = screamer_mmio_write,
  749. .endianness = DEVICE_LITTLE_ENDIAN,
  750. .valid = {
  751. .min_access_size = 4,
  752. .max_access_size = 4
  753. }
  754. };
  755. /* Called when the device has become active */
  756. static void screamer_realize(DeviceState *dev, Error **errp)
  757. {
  758. DPRINTF("screamer_realize() called\n");
  759. screamer_reset(dev);
  760. }
  761. /*
  762. * Called when an instance of the Screamer device is created.
  763. * Also called when this HMP command is called: device_add screamer
  764. */
  765. static void screamer_init(Object *obj)
  766. {
  767. DPRINTF("screamer_init() called\n");
  768. ScreamerState *s = (ScreamerState *)obj;
  769. SysBusDevice *d = SYS_BUS_DEVICE(obj);
  770. const int region_size = 5 * 32;
  771. /* Makes the read and write ops work */
  772. memory_region_init_io(&s->io_memory_region, OBJECT(s),
  773. &screamer_ops, s, SOUND_CHIP_NAME, region_size);
  774. /* Sets the SysBusDevice's memory property */
  775. sysbus_init_mmio(d, &s->io_memory_region);
  776. /* Setup all the interrupt requests */
  777. sysbus_init_irq(d, &s->irq);
  778. sysbus_init_irq(d, &s->dma_send_irq);
  779. sysbus_init_irq(d, &s->dma_receive_irq);
  780. /* Registers Screamer with QEMU's audio system */
  781. AUD_register_card(SOUND_CHIP_NAME, &s->card);
  782. }
  783. /*
  784. * When saving and restoring the state of the VM, this is used to save and
  785. * restore the registers.
  786. */
  787. static const VMStateDescription vmstate_screamer = {
  788. .name = "Screamer",
  789. .version_id = 1,
  790. .minimum_version_id = 1,
  791. .fields = (VMStateField[]) {
  792. VMSTATE_UINT16_ARRAY(awacs, ScreamerState, 8), /* 8 AWACS registers */
  793. VMSTATE_UINT32(sound_control, ScreamerState),
  794. VMSTATE_UINT32(codec_control, ScreamerState),
  795. VMSTATE_UINT32(codec_status, ScreamerState),
  796. VMSTATE_UINT32(clip_count, ScreamerState),
  797. VMSTATE_UINT32(byte_swap, ScreamerState),
  798. VMSTATE_UINT32(frame_count, ScreamerState),
  799. VMSTATE_END_OF_LIST()
  800. }
  801. };
  802. /*
  803. * Sets the class data. It is like polymorphism and inheritance in object
  804. * oriented languages.
  805. */
  806. static void screamer_class_init(ObjectClass *class, void *data)
  807. {
  808. DPRINTF("screamer_class_init() called\n");
  809. DeviceClass *dc = DEVICE_CLASS(class);
  810. dc->realize = screamer_realize;
  811. dc->reset = screamer_reset;
  812. dc->desc = "Apple Screamer";
  813. dc->vmsd = &vmstate_screamer;
  814. dc->hotpluggable = false;
  815. }
  816. /* Used for QOM function registration */
  817. static const TypeInfo screamer_info = {
  818. .name = "screamer",
  819. .parent = TYPE_SYS_BUS_DEVICE,
  820. .instance_size = sizeof(ScreamerState),
  821. .instance_init = screamer_init,
  822. .class_init = screamer_class_init,
  823. };
  824. /* QOM registration of above functions for calling */
  825. static void screamer_register_types(void)
  826. {
  827. DPRINTF("screamer_register_types() called\n");
  828. type_register_static(&screamer_info);
  829. }
  830. /* QEMU Object Model (QOM) stuff */
  831. type_init(screamer_register_types)