<HTML>
<style> BODY { font-family:Arial, Helvetica, sans-serif;font-size:12px; }</style>Hi Alexander,<br>
<br>
<div>Pressing Ctrl+C or Ctrl+Break is an interrupt that breaks normal program flow - if unhandled, it causes an abnormal program termination and things don't get cleaned up properly, which includes calling finalization blocks.<br>
</div><div><br>
</div><div>Try doing these steps to get around it:<br>
<br>
- Include the "SysUtils" unit. This causes the Ctrl+C signal to raise an exception which can then be trapped.<br>
- Instead of using 'finalization', wrap your main procedure with a 'try...finally' block, calling "spkoff" in the finally section.</div><div><br>
</div><div><br>
</div><div>As an extra note... if you specify the "assembler" directive after your procedure header, you don't need to use "begin...end" and can just use "asm...end" directly.</div><div><br>
</div><div>Let me know how it goes.<br>
</div><div><br>
</div><div>Gareth aka. Kit<br>
</div><br>
<br>
<br>
<span style="font-weight: bold;">On Tue 22/01/19 09:11 , "Alexander via fpc-devel" fpc-devel@lists.freepascal.org sent:<br>
</span><blockquote style="BORDER-LEFT: #F5F5F5 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">Hi,
<br>
<br>
I try use finalization in unit, but it not work.
<br>
<br>
When press Ctrl+C and exit program uses this unit
<br>
PC-Speaker stay infinity play on interrupted tone.
<br>
When Ctrl+C use spkoff; in finalization section not called.
<br>
<br>
Unit:
<br>
<br>
unit spkunit;
<br>
<br>
{$MODE OBJFPC}
<br>
{$ASMMODE INTEL}
<br>
{$CODEPAGE UTF8}
<br>
<br>
{
<br>
Unit for playing melodys on PC-Speaker.
<br>
For GNU/Linux 64 bit version. Root priveleges needed.
<br>
Written on FreePascal.
<br>
Copyright (C) 2019 Artyomov Alexander
<br>
<br>
This program is free software: you can redistribute it and/or modify
<br>
it under the terms of the GNU Affero General Public License as
<br>
published by the Free Software Foundation, either version 3 of the
<br>
License, or (at your option) any later version.
<br>
<br>
This program is distributed in the hope that it will be useful,
<br>
but WITHOUT ANY WARRANTY; without even the implied warranty of
<br>
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
<br>
GNU Affero General Public License for more details.
<br>
<br>
You should have received a copy of the GNU Affero General Public License
<br>
along with this program. If not, see <<a target="_blank" href="parse.php?redirect=https://www.gnu.org/licenses/%26gt"><span style="color: red;">https://www.gnu.org/licenses/></span></a>;.
<br>
}
<br>
<br>
interface
<br>
<br>
uses X86;
<br>
<br>
procedure spk(b : word ); procedure spkon; procedure spkoff;
<br>
<br>
implementation
<br>
<br>
procedure spkon;
<br>
begin
<br>
asm
<br>
push rax
<br>
in al, 61h
<br>
or al, 03h
<br>
out 61h, al
<br>
pop rax
<br>
end;
<br>
end;
<br>
procedure spkoff;
<br>
begin
<br>
asm
<br>
push rax
<br>
in al, 61h
<br>
or al, 03h
<br>
xor al, 03h
<br>
out 61h, al
<br>
pop rax
<br>
end;
<br>
end;
<br>
procedure spk(b : word );
<br>
var hb, lb : byte;
<br>
begin; hb := hi (b); lb := lo (b);
<br>
asm
<br>
push rax
<br>
mov al, 0B6h
<br>
out 43h, al
<br>
mov al, lb
<br>
out 42h, al
<br>
mov al, hb
<br>
out 42h, al
<br>
pop rax
<br>
end;
<br>
end;
<br>
<br>
initialization
<br>
<br>
fpioperm($42, 2, 1); // fpioperm($42, 1, 1); fpioperm($43, 1, 1);
<br>
fpioperm($61, 1, 1);
<br>
<br>
finalization
<br>
<br>
spkoff;
<br>
<br>
end.
<br>
<br>
_______________________________________________
<br>
fpc-devel maillist - <a href="javascript:top.opencompose('fpc-devel@lists.freepascal.org','','','')">fpc-devel@lists.freepascal.org</a>
<br>
<a target="_blank" href="parse.php?redirect=<a href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</a>"><span style="color: red;">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</span></a>
<br>
<br>
<br>
</blockquote></HTML>