Hi All,
I have a need to send a byte out to an I/O port to turn off the LEDs after powering up the computer. Whilst I can do a one-liner in mbasic to achieve this, I wanted to include a command to do this in the profile.sub file.
So...in the interest of re-inventing the wheel and learning new stuff, I whipped up a quick and dirty Turbo Pascal program to send an byte to an I/O port. It uses inline assembler and command line processing which took a little while to figure out and may be useful as an example for others.
I have compiled the program as
setport.com and can either call it with or without parameters.
E>setport
Output byte to a port
Port number > $02
Output value > $55
E>setport $02 $00
E>
I have used hex notation (leading $) in the example above however you can use ordinary decimal numbers as well.
There are no comments (remember it's quick and dirty) and it probably violates all sorts of sensibilities! There is minimal error checking as well.
program SetPort;
var Error, PortNumber, OutValue: Integer;
var PortStr, ValueStr: String[3];
procedure OutPort(Port: Byte; Value: Byte);
begin
inline ($3A/Port/ { ld a,Port }
$4F/ { ld c,a }
$3A/Value/ { ld a,(Value) }
$ED/$79 { out (c),a });
end;
procedure ErrorAndExit(ErrorCode: Integer);
begin
writeln('An error has occured. Error code is ', ErrorCode);
exit;
end;
begin
if ParamCount = 2 then
begin
PortStr := ParamStr(1);
ValueStr := ParamStr(2);
val(PortStr, PortNumber, Error);
if Error <> 0 then
ErrorAndExit(Error);
val(ValueStr, OutValue, Error);
if Error <> 0 then
ErrorAndExit(Error);
end
else
begin
writeln('Output byte to a port');
write('Port number > ');
readln(PortNumber);
write('Output value > ');
readln(OutValue);
end;
OutPort(PortNumber, OutValue);
end.
Enjoy!