Category : Modula II Source Code
Archive   : M2MUSIC.ZIP
Filename : SPEAKER.MOD

 
Output of file : SPEAKER.MOD contained in archive : M2MUSIC.ZIP
IMPLEMENTATION MODULE Speaker;
(* Routines turn the speaker on at a given frequency,
to rurn it off again, and to delay processing for a set interval.
For specifications see corresponding definition module.
Written for FST Modula-2 by Robin Roos and Pat Terry, November 1988 *)

FROM SYSTEM IMPORT ASSEMBLER;

CONST
Channel2 = 42H;
CommandReg = 43H;
PortB = 61H;

PROCEDURE Sound (FreqHz : CARDINAL);
BEGIN
ASM
PUSH DS
MOV BX,FreqHz
MOV DX,0012H
MOV AX,34DDH (* DX:AX to 1.193181 MHz *)
CMP DX,BX
JNB exit (* safety check *)
DIV BX
MOV BX,AX (* save counter *)
IN AL,PortB
TEST AL,03H (* already on ? *)
JNZ play
OR AL,03H (* turn on 2 low bits *)
OUT PortB,AL
MOV AL,0B6H (* 1011 0110 bit pattern for channel 2 *)
OUT 43H,AL
play:
MOV AL,BL
OUT Channel2,AL (* LSB of counter *)

MOV AL,BH
OUT Channel2,AL (* MSB of counter *)
exit:
POP DS
END;
END Sound;

PROCEDURE NoSound;
BEGIN
ASM
PUSH DS
IN AL,PortB
AND AL,0FCH (* 1111 1100 force two low bits to zero *)
OUT PortB,AL
POP DS
END;
END NoSound;

PROCEDURE PlayNote (Frequency, Time : CARDINAL);
BEGIN
Sound(Frequency);
Delay(Time);
NoSound;
END PlayNote;

PROCEDURE Delay (Time : CARDINAL);
BEGIN
Time := SHORT((182065L * LONG(Time)) DIV 10000000L);
IF Time = 0 THEN RETURN END;
ASM
PUSH DS
MOV AH,0
INT 1AH
ADD DX,Time
MOV BX,DX
repeat:
INT 1AH
CMP DX,BX
JNE repeat
POP DS
END
END Delay;

PROCEDURE ATDelay (Time : CARDINAL);
VAR
Hi, Lo : CARDINAL;
T : LONGCARD;
BEGIN
T := LONG(Time) * 1000L (* microseconds *);
IF T = 0L THEN RETURN END;
Hi := SHORT(T DIV 65535L); Lo := SHORT(T MOD 65535L);
ASM
PUSH DS
MOV DX,Lo
MOV CX,Hi
MOV AH,86H
INT 15H
POP DS
END
END ATDelay;

PROCEDURE Wait (Time : CARDINAL);
CONST
DelayFactor = 0290H (* rough value for AT - but m/c dependent *);
BEGIN
ASM
PUSH DS
MOV CX,Time
JCXZ Exit
Start:
PUSH CX
MOV CX,DelayFactor
Inner:
LOOP Inner (* Delay One Millisecond *)
POP CX
LOOP Start (* Each of these LOOPs gives one more millisecond *)
Exit:
POP DS
END;
END Wait;

END Speaker.







  3 Responses to “Category : Modula II Source Code
Archive   : M2MUSIC.ZIP
Filename : SPEAKER.MOD

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/