flat assembler
Message board for the users of flat assembler.

Index > DOS > Internal speaker problem in WinXP [SOLVED]

Author
Thread Post new topic Reply to topic
DimonSoft



Joined: 03 Mar 2010
Posts: 821
Location: Belarus
DimonSoft
I've written a small piece of code for my tomorrow lecture, which uses internal speaker. Its purpose is just to show something cool and simple using I/O-ports.

Code:
     org 100h

Start:
     ; Setting up timer 2
     mov     al, $B6
     out     $43, al

     mov     al, $D0
     out     $42, al
     mov     al, $11
     out     $42, al

     ; Sound
     in      al, $61
     or      al, 3
     out     $61, al

     ; Delay
     xor     ax, ax
     mov     es, ax
     mov     ax, [es:$046C]
     mov     cx, 9
.WaitLoop:
     mov     dx, [es:$046C]
     cmp     ax, dx
     je      .WaitLoop
     mov     ax, dx
     dec     cx
     jnz     .WaitLoop

     ; NoSound
     in      al, $61
     and     al, $FC
     out     $61, al

     ret    


If I use cmd.exe to run the program, everything works fine: I hear a sound for about half a second, then the program terminates. But if I try to run the program from FASM (F9) or Explorer (double-click), the program behaves exactly the same way, except it doesn't produce any sound. I guess, there're some differences in the way NTVDM behaves in these cases.

Is there a way to make the program work properly when run from Explorer/FASM?

P.S. Programs in Basic (there's a PLAY procedure in Basic) compiled to EXE work fine no matter where they're run from. But AFAIK they also change timer 0 options. Is that related to the problem?


Last edited by DimonSoft on 14 Mar 2013, 23:54; edited 2 times in total
Post 14 Mar 2013, 15:33
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17667
Location: In your JS exploiting you and your system
revolution
You could also use the native Windows API for making sounds. AFAIK NTVDM emulates the hardware anyway so you are not gaining anything by using a DOS version.
Post 14 Mar 2013, 15:42
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 821
Location: Belarus
DimonSoft
revolution wrote:
You could also use the native Windows API for making sounds. AFAIK NTVDM emulates the hardware anyway so you are not gaining anything by using a DOS version.

The problem is that the students are currently assumed to know only real-mode DOS-programming. And the lecture is about using I/O-ports. Using PC speaker seems to be the most attractive way to show it in practice.

P.S. Noted that even when run from cmd.exe the program doesn't produce sound for the first time. Starting from the second run everything works fine.
Post 14 Mar 2013, 16:16
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 821
Location: Belarus
DimonSoft
Hope it helps someone.

To make the code work properly, you should connect the timer channel 2 to speaker (done by the piece of code marked «Sound») BEFORE setting up the timer (done by the piece of code marked «Setting up timer 2»).

Could anyone, please, give some comments on this problem? Maybe links to some documentation where this fact is stated explicitly?[/b]
Post 14 Mar 2013, 17:37
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
DimonSoft,

That's probably a quirk of 8253 emulation by NTVDM. DOSBox runs your original code fine, with beep each time.

IIRC only modes 1 and 5 require trigger (i.e. rising GATE edge) to start counting. In mode 3 PIT is specified to operate similar in either case (whether GATE is asserted before or after CW/count programming).

PIT output (i.e. OUT2 pin) is anded with bit 1 of port 61; GATE2 input is connected to bit 0 of that port.

For a reference, you may read 82C54 datasheet.
Post 14 Mar 2013, 21:33
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 821
Location: Belarus
DimonSoft
baldr wrote:
DimonSoft,

That's probably a quirk of 8253 emulation by NTVDM. DOSBox runs your original code fine, with beep each time.

IIRC only modes 1 and 5 require trigger (i.e. rising GATE edge) to start counting. In mode 3 PIT is specified to operate similar in either case (whether GATE is asserted before or after CW/count programming).

PIT output (i.e. OUT2 pin) is anded with bit 1 of port 61; GATE2 input is connected to bit 0 of that port.

For a reference, you may read 82C54 datasheet.

Thanks, I'll give it a look tomorrow.
Post 14 Mar 2013, 23:52
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
DimonSoft wrote:
problem is that the students are currently assumed to know only real-mode DOS


Please do your students a favor and tell them the truth.
Post 27 Mar 2013, 06:50
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 821
Location: Belarus
DimonSoft
DOS386 wrote:
DimonSoft wrote:
problem is that the students are currently assumed to know only real-mode DOS


Please do your students a favor and tell them the truth.

Smile

<OffTopic>
Actually the problem is that starting with, say, Windows is a bit more difficult, 'cause you have to learn both assembler and WinAPI at the same time. It turns out that starting with DOS programming is much easier.

Anyway, I currently teach them unofficially (until the next year), so I can't change the list of topics and can't make teachers who check their labs understand protected-mode programming (they're too old for that stuff). The only thing I can do is to give more practical examples than theory, and that's what I actually try to do.
</OffTopic>
Post 27 Mar 2013, 14:52
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.