From ccc5a29ef8d2569f9e22c2e641d929897a06cd79 Mon Sep 17 00:00:00 2001 From: rnlf Date: Mon, 23 Jan 2017 21:32:50 +0100 Subject: [PATCH] Better timing behaviour when resetting blaster. Only deinit blaster if init was successful. --- src/mixer.c | 2 +- src/mixer.h | 2 +- src/modules/l_sound.c | 2 +- src/modules/l_source.c | 2 +- src/soundblaster.c | 38 +++++++++++++++++++++++++++++++------- src/soundblaster.h | 2 +- src/source.c | 2 +- src/source.h | 2 +- src/wavefile.c | 2 +- src/wavefile.h | 2 +- 10 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/mixer.c b/src/mixer.c index d24367b..891b359 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. diff --git a/src/mixer.h b/src/mixer.h index f442956..e11c9bd 100644 --- a/src/mixer.h +++ b/src/mixer.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. diff --git a/src/modules/l_sound.c b/src/modules/l_sound.c index 643b52e..eee6994 100644 --- a/src/modules/l_sound.c +++ b/src/modules/l_sound.c @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. diff --git a/src/modules/l_source.c b/src/modules/l_source.c index 21544ab..a043da1 100644 --- a/src/modules/l_source.c +++ b/src/modules/l_source.c @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. diff --git a/src/soundblaster.c b/src/soundblaster.c index 18d413e..8950b1a 100644 --- a/src/soundblaster.c +++ b/src/soundblaster.c @@ -1,10 +1,11 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. */ +#include #include #include #include @@ -86,6 +87,7 @@ static uint16_t irq; static uint16_t dmaChannel; static bool isrInstalled = false; static int writePage = 0; +static bool blasterInitialized = false; static _go32_dpmi_seginfo oldBlasterHandler, newBlasterHandler; static soundblaster_getSampleProc getSamples; @@ -108,10 +110,27 @@ static uint8_t readDSP() { } +static inline void delay3us() { + uint64_t waited = 0; + uclock_t lastTime = uclock(); + while(waited < (3*UCLOCKS_PER_SEC) / 1000000) { + uclock_t nowTime = uclock(); + + // Just ignore timer wraps. In the worst case we get a slightly + // longer delay, but who cares? + if(nowTime > lastTime) { + waited += nowTime - lastTime; + } + + lastTime = nowTime; + } +} + + static int resetBlaster(void) { for(int j = 0; j < 1000; ++j) { outportb(baseAddress + BLASTER_RESET_PORT, 1); - delay(1); + delay3us(); outportb(baseAddress + BLASTER_RESET_PORT, 0); if(readDSP() == BLASTER_READY_BYTE) { @@ -334,6 +353,8 @@ int soundblaster_init(soundblaster_getSampleProc getsamplesproc) { turnSpeakerOn(); startDMAOutput(); + blasterInitialized = true; + return 0; } @@ -362,9 +383,12 @@ static void stopDMAOutput(void) { void soundblaster_deinit(void) { - turnSpeakerOff(); - stopDMAOutput(); - resetBlaster(); - resetBlasterISR(); - deallocSampleBuffer(); + if(blasterInitialized) { + turnSpeakerOff(); + stopDMAOutput(); + resetBlaster(); + resetBlasterISR(); + deallocSampleBuffer(); + blasterInitialized = false; + } } diff --git a/src/soundblaster.h b/src/soundblaster.h index f6b7841..4281476 100644 --- a/src/soundblaster.h +++ b/src/soundblaster.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. diff --git a/src/source.c b/src/source.c index 9b56c9e..f792244 100644 --- a/src/source.c +++ b/src/source.c @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. diff --git a/src/source.h b/src/source.h index 9282265..95df48e 100644 --- a/src/source.h +++ b/src/source.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. diff --git a/src/wavefile.c b/src/wavefile.c index ef1e4b2..8fe4168 100644 --- a/src/wavefile.c +++ b/src/wavefile.c @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. diff --git a/src/wavefile.h b/src/wavefile.h index f462a27..24a2044 100644 --- a/src/wavefile.h +++ b/src/wavefile.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 rnlf + * Copyright (c) 2017 Florian Kesseler * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details.