Simple Turbo Pascal program to output byte to an I/O port

592 views
Skip to first unread message

Gary Hammond

unread,
Apr 13, 2021, 11:46:23 PM4/13/21
to RC2014-Z80
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!

Stuart Smith

unread,
Apr 14, 2021, 12:07:36 AM4/14/21
to rc201...@googlegroups.com
You could have done item mbasic and compiled it to get the same result. 

--
You received this message because you are subscribed to the Google Groups "RC2014-Z80" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rc2014-z80+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/rc2014-z80/bde67f2e-47a5-431e-8bf4-0fd00cd8dbean%40googlegroups.com.

Gary Hammond

unread,
Apr 14, 2021, 12:15:11 AM4/14/21
to RC2014-Z80
I sure could have, but where's the fun in that! Part of the exercise was to learn some Pascal.

What's the speed of compiled mbasic like compared to Pascal?

Doug Jackson

unread,
Apr 14, 2021, 12:21:54 AM4/14/21
to rc201...@googlegroups.com
Why would you learn Pascal, when you could learn the One True Language (tm)

FORTH.


(duck)


Kindest regards,

Doug Jackson

ph: 0414 986878

Check out my awesome clocks at www.dougswordclocks.com
Follow my amateur radio adventures at vk1zdj.net



Stuart Smith

unread,
Apr 14, 2021, 12:33:08 AM4/14/21
to rc201...@googlegroups.com
The last time I checked, back in the 80's, it was marginally faster than mbasic. Back then if I needed performance, I would write in assembler. If it was a bit complex, I'd write in turbo pascal and mbasic if I had the write it fast. Incidentally, I had access to an IBM XT at the time and both mbasic and turbo pascal were faster on a 4MHz Z80 than the 4.77MHz XT. The XT was about 60% the speed of my Z80 system. 

Richard Deane

unread,
Apr 14, 2021, 2:30:35 AM4/14/21
to rc201...@googlegroups.com
Or you could have stayed with Digital Research products and used PLI-80; genuinely retro.
Richard 

Benjamin Scherrey

unread,
Apr 14, 2021, 3:25:31 AM4/14/21
to rc201...@googlegroups.com
Actually forth is the ideal language/environment for this kind of thing. Would be cool to see more rc-2014 folk become forthwrights as well! :-)

(But TurboPascal is also quite good if you want a classic compiled language. Borland created an amazing little product.)

  -- Ben

J.B. Langston

unread,
Apr 14, 2021, 10:13:02 AM4/14/21
to RC2014-Z80
Turbo Pascal has a built-in 256-byte array called Port that allows you to write to ports without resorting to assembly.  There is also one called Mem that allows you to write to any location in memory.  See page 290: http://bitsavers.informatik.uni-stuttgart.de/pdf/borland/turbo_pascal/Turbo_Pascal_Version_3.0_Reference_Manual_1986.pdf

Gary Hammond

unread,
Apr 18, 2021, 1:45:12 AM4/18/21
to RC2014-Z80
I should have RTFM. When I went back and had a look, it was there between a couple of the sections I had read for other reasons. Thanks for the tip.

John Kennedy

unread,
Jul 11, 2021, 9:30:27 PM7/11/21
to RC2014-Z80
Very helpful! The IMSAI8080esp can execute code called over the network by sending text to the TTY: device, and so using this I can use Python to control the Programmed Output LEDs!
Also, I didn't realize that Z80 could be embedded in Turbo Pascal. Nice!

Nigel Kendrick

unread,
Jul 12, 2021, 1:12:03 AM7/12/21
to RC2014-Z80
Here's one I prepared earlier:

Nick Bolton

unread,
Jul 1, 2022, 8:06:19 AM7/1/22
to RC2014-Z80
I extended the OP's code. Using inline machine code in Pascal is quite interesting!

After reading the BASIC code on the RC2014 Digital IO page (https://rc2014.co.uk/modules/digital-io/), I decided that I'd rather use something like Pascal. Turns out it's as simple as using the port array in Pascal, but the OP of this thread sparked my curiosity about inline machine code in Pascal. Given that you can output to a port using inline machine code, I wondered if you could read a port using similar machine code. Turns out you can!

I had a lot of help...

Nick

Reply all
Reply to author
Forward
0 new messages