diff --git a/abstract-machine/am/src/platform/nemu/ioe/audio.c b/abstract-machine/am/src/platform/nemu/ioe/audio.c index 98c2478..a3e0519 100644 --- a/abstract-machine/am/src/platform/nemu/ioe/audio.c +++ b/abstract-machine/am/src/platform/nemu/ioe/audio.c @@ -10,6 +10,7 @@ static uint32_t audio_write = 0; static uint32_t audio_sbuf_size; +#define MIN(a, b) ((a) < (b) ? (a) : (b)) void __am_audio_init() { } @@ -39,16 +40,15 @@ void __am_audio_play(AM_AUDIO_PLAY_T *ctl) { while (cnt) { // lock sbuf outl(AUDIO_INIT_ADDR, 2); - for (; i < cnt; i++) { - if (inl(AUDIO_COUNT_ADDR) == audio_sbuf_size) { - i--; - break; - } else { - outb(AUDIO_SBUF_ADDR + audio_write, ((uint8_t*)ctl->buf.start)[i]); - audio_write = (audio_write + 1) % audio_sbuf_size; - outl(AUDIO_COUNT_ADDR, inl(AUDIO_COUNT_ADDR) + 1); - } + + int last = audio_sbuf_size - inl(AUDIO_COUNT_ADDR); + int write_num = MIN(last, cnt); + for (int j = i; j < write_num; i ++, j ++) { + outb(AUDIO_SBUF_ADDR + audio_write, ((uint8_t*)(ctl->buf.start))[i]); + audio_write = (audio_write + 1) % audio_sbuf_size; } + cnt -= write_num; + outl(AUDIO_COUNT_ADDR, inl(AUDIO_COUNT_ADDR) + write_num); // unlock sbuf outl(AUDIO_INIT_ADDR, 0); }