Modern Object Pascal guide

Link to Quick Modern Object Pascal for Programmers

Michalis Kamburelis and contributors: Maciej Izak

There are many books and resources about Pascal out there, but too many of them talk about the old Pascal, without classes, units or generics.

So I wrote this quick introduction to what I call modern Object Pascal. Most of the programmers using it don’t really call it “modern Object Pascal”, we just call it “our Pascal”. But when introducing the language, I feel it’s important to emphasize that it’s a modern, object-oriented language. It evolved a lot since the old (Turbo) Pascal that many people learned in schools long time ago. Feature-wise, it’s quite similar to C++ or Java or C#.

  • It has all the modern features you expect — classes, units, interfaces, generics…
  • It’s compiled to a fast, native code,
  • It’s very type safe,
  • High-level but can also be low-level if you need it to be.

It also has excellent, portable and open-source compiler called the Free Pascal Compiler, http://freepascal.org/ . And an accompanying IDE (editor, debugger, a library of visual components, form designer) called Lazarushttp://lazarus.freepascal.org/ . Myself, I’m the creator of Castle Game Engine, http://castle-engine.sourceforge.net/ , which is a cool portable 3D and 2D game engine using this language to create games on many platforms (Windows, Linux, MacOSX, Android, iOS, web plugin).

This introduction is mostly directed at programmers who already have experience in other languages. We will not cover here the meanings of some universal concepts, like “what is a class”, we’ll only show how to do them in Pascal.

See here!

PCMCIA Parallel cards

Modern notebooks do not come anymore with parallel ports. If you are luck and have a docking station, perhaps. Otherwise no more fun with LPT interfacing alas.
So I had a look at USB or Cardbus/PCMCIA solutions.

Do not bother with the USB to LPT adapters. Those are capable of driving a printer, but the interface as is required for the programming here is not there.
There are many Cardbus Parallel port adapters and most also do not work as I want.
I did find one, and it works fine. It is the Delock PCMCIA Cardbus 1 x Parallel adapter.

Ofcourse it has the big DB25 pin connector as you can see, it is really standard.

Drivers are either found at the driver cd or (more uptodate) at Delock’s website. Windows 7 drivers are installed automatically. As you can see, the card is based on a MosChip IC.

Now not everything is simple as with the oldfashioned parallel port.
As you can see in the Properties tab (Device Manager shows this) for this instance it is LPT3 and the I/O range is starting at FFF0. Depending on (I really dont have a clue!) it can also be other I/O resource addresses and/or LPT port numbers. It is the same after every reboot, so it is only once determined.
Substitute this in your program (and remove the sanity checks if any for valid ports).

My tests has shown this to work as a standard LPT port. It is a bit slower alas.

LPT Speed

Checks maximum speed that can be obtained by reading from the LPT port.
5,000,000 bytes are read into a buffer in a loop as fast as possible.

Time elapsed and obtained speed of sampling are reported.
Requires port95nt.exe (DLPORTIO) on Windows 32 bit, see here for 64 bit.

Run it multipe time to get an impression of the accuracy that can be obtained, with the rest of Windows interfering. The code contains thread and process priority raised to as high levels as possible, the influence of this is minor but visible.

Test results so far:
– Pentium IV notebook Windows 98 Se 650 kHz (648-652)
– Gateway 7 (Pentium III 450) 680 kHz (678-681)
– DELL Optiplex Dual Core 5400 2.3 GHz 720 kHz (719-721)

It seems that the parallel port architecture + DLPORTIO limits the speed of sampling.

Program + source of LPT Speed (Delphi 7).

The procedure doing the sampling is shown here. Note that the port has to be set to input, see the parallel port page for information on that.

procedure TLPTSpeed.bcaptureClick(Sender: TObject);

var
  start,stop, freq:int64;
  elapsed : extended ;
  buffercount :cardinal ;

begin
  QueryPerformanceFrequency(freq); {Get frequency}

  buffercount := 1 ;
  screen.cursor:=crHourGlass; {show busy cursor}

  {collect data }
  SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS   );
  SetThreadPriority( GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL  );
  QueryPerformanceCounter(start); {Get initial count}
  repeat
    buffer[buffercount] := DlPortReadPortUchar($378) ;
    buffercount := buffercount + 1 ;
  until buffercount = bufferlen ;
  QueryPerformanceCounter(stop);

  SetPriorityClass( GetCurrentProcess, NORMAL_PRIORITY_CLASS );
  SetThreadPriority( GetCurrentThread, THREAD_PRIORITY_NORMAL  );
    screen.cursor:=crDefault;  {show normal cursor}

  { report }
  elapsed := ((stop - start) * 1000) / freq  ;
  log.lines.add(FormatFloat('0.00',(elapsed))+ ' ms') ;
  elapsed := bufferlen /  (elapsed)      ;
  log.lines.add('Frequency = ' +  FormatFloat('0.00',elapsed) + 'kHz') ;
end;

Relay card

An 8 relay card for the parallel port. This is a better type, since it has its own buffer, so you can detach the PC after setting/clearing the relays. Most parallel port relay card do not have a buffer, so they are easy to program, just write a value to the data port.

With the TESTLPT program I discovered this card behaves like a printer:
– set the wanted setting on the data pins
– change the strobe output for a short period
– hear the relays click to the requested state

To really test this nice piece of hardware I wrote a barebones program. It can turn on and off each relay, or all relay on and off with one click.
Ofcourse a real application requires a scheduler with programmable activities. Maybe I will add that later, this is enough to test the device and my skills to program it myself.

The LPT Relay program as Windows installable.

The sources of LPT Relay

Screenshot:

Logic analzyer

digitrac_scheme_active

Original circuit diagram

The TFLA is a 8 port logic analyzer. Connected to the parallel port of a fast PC, the sampling rate can reach 1 MHz, And that makes it usable for my 6502 systems! Also perfect to analyze serial transmission.

I have built the TFLA, the acronym for The Fabulous Logic Analyzer.
Not my own design, here is the original design, here the Windows program and here the source and design.

The TFLA is a 8 port logic analyzer. Connected to the parallel port of a fast PC, the sampling rate can reach 1 MHz, And that makes it usable for my 6502 systems! Also perfect to analyze serial transmission.

I made some changes to the original design:
– Between the parallel port and the output of the buffer I added 1 K resistors. To protect PC ports that are not bidirectional.
– Added 100 ohm resistors between the input and the buffer. To protect the buffer.
– Added 10k ohm resistors to ground at the input of the buffer. This makes sure an unconnected input is at null level.

– Power for the buffer IC is taken from the USB port. Also this +5V is brought to the outside, enough to provide power for experiments.

– The buffer is a 74HCT245, a perfect low power high impedance buffer IC.
– Connection between the TFLA and the parallel port via a Centronics connector (from an old printer!) and a standard printer cable.
– The connectors for the logic input signals are from Conrad (2.6 mm) 730599 – 89 and 733628 – 89 (PCB bus).
– The clips are from dealextreme.com
– The simple circuit is built on a strip-board.

Here are the photos:

tfla

tfla

tfla

tfla

tfla

Test the parallel port

To be able to read and write from the PC ports like the parallel port, one needs to understand it workings. And a way to circumvent the operating system from accessing directly the ports.

This document describers all you need to know about the parallel port in bidirectional mode.

Elektor magazine published an article on testing the parallel port with a device showing with LEDs the state of every pin of the port. I have build one and its very handy testing devices and in general the elementary programs for the parallel port.

DLPORTIO is a driver and API to read and write ports in a PC under Windows 95/98/2000/XP.

DLPORTIO.PAS is a unit for Delphi/FreePascal with the API to DLPORTIO. See this page for Windows 64!

To combine all this knowledge and get started again with Delphi I have written a simple but effective program, TESTLPT, to read and write all ports relevant for the parallel port.

Here you find TESTLPT, complete with DLPORTIO , all automatically installed in Windows.

Sources of TESTLPT

Turbo Pascal and Delphi

gr782dia266

Delphi, Greece, temple of the oracle, visit 1978

Turbo Pascal 3 is an IDE and compiler in one package. It runs on CP/M and MS-DOS and the CP/M compatible MSX-DOS. A blazing fast compiler which generated compact and fast executables.

See this page http://turbopascal.org/ how the Turbo Pascal 3.0 works, Complete documented source!

Borland added a lot of functionality, such as object orientation and modularity via units in Borland Pascal 4 to 7. The conversion to Windows was made via Borland Pascal for Windows and then Delphi. Still the same object oriented Pascal programming language.

Delphi is an integrated development environment (IDE) for console, desktop graphical, web applications

Earlier Delphi’s compilers use their own Object Pascal dialect of Pascal and generate native code for Windows NT (IA-32 and x64).

Delphi was originally developed by Borland as a rapid application development tool for Windows, and as the successor of Borland Pascal. Delphi and its C++ counterpart, C++Builder, shared many core components, notably the IDE and the Visual Component Library (VCL), but remained separate until the release of RAD Studio 2007. RAD Studio is a shared host for Delphi, C++Builder, and others.

In 2006, Borland’s developer tools section were transferred to a wholly owned subsidiary known as CodeGear, which was sold to Embarcadero Technologies in 2008.

I still use Delphi 7, later versions are too expensive for me. It still runs on current versions of Windows with some help, it was aimed for Windows XP.

Delphi

gr782dia266

Delphi, Greece, visited in 1978. The temple where the oracle was located.

Delphi is an integrated development environment (IDE) for console, desktop graphical, web, and mobile applications.[1]

Delphi’s compilers use their own Object Pascal dialect of Pascal and generate native code for several platforms: Windows NT (IA-32 and x64), OS X (IA-32 only), iOS and Android.

Delphi was originally developed by Borland as a rapid application development tool for Windows, and as the successor of Borland Pascal. Delphi and its C++ counterpart, C++Builder, shared many core components, notably the IDE and the Visual Component Library (VCL), but remained separate until the release of RAD Studio 2007. RAD Studio is a shared host for Delphi, C++Builder, and others.

In 2006, Borland’s developer tools section were transferred to a wholly owned subsidiary known as CodeGear, which was sold to Embarcadero Technologies in 2008.

I have followed the Delphi upgrades up till Delphi 7, aimed at Windows XP. The price became too high after that version. It still runs (with some help) on current Windows versions.

DriverLynx DLPORTIO and Windows 7 and 8 and 64 bit

DriverLynx PortIO (DLLPORT.SYS) is needed on Windows NT and higher to access the I/O ports in a PC from a program, for example in Delphi.

Download here the driver installation and the Pascal unit.

Working examples, including the driverlynx driver and with Delphi example sources are on my Programs and PC ports pages

Alas, Windows 7 (and Vista) make it a bit more difficult to install this driver. It seems everything is allright running the installer (port95nt.exe), but running the program gives the error DLPORTIO.SYS is not loaded.

But it can be done as follows:

  • Right Click Port95nt.exe
    – Properties
    Compatibility
    – Run this program in compatibility mode for: Windows XP Service Pack 2
    – Run this program as an administrator

And now it does install and the program runs.

There is now a version of the inpout library DLPORTIO, usable form Delphi 7 as I have shown before, also for 64 bit (Windows 7 and 8.1):

http://www.highrez.co.uk/downloads/inpout32/

From Version 1.0.0.7, InpOut provides basic DLPortIO compatibility.

To use it, take the 32bit DLL (in \Windows x86) and rename it to DLPortIO.dll

Delphi 7 and Windows 7 and 8.1 32 and 64 bit

Delphi Object Pascal. The version I use (since it is the last version close to traditional Windows programming) is version 7. The best version to use, is the version that floats around as Delphi 7.2 Second Edition 42 MB (search a torrent site) which is a trimmed and bugfixed small package.

Of course this package runs fine on Windows XP. But how to install on Windows 7, 8, 64 bit?

This is how I got it to work on Windows 7 and Windows 8.1 64 bit.

Installation

  • Make sure you run this as administrator, you will need the privs.
  • Run the Delphi installer (in compatibility mode if errors). Use defaults for the file locations, ignore the incompability warnings.
  • Do not start Delphi 7 as it will complain when starting a project about unable to rename delphi32.$$$ to .dro in the /program files/delphi directories . And something about debug options requiring a change.

So there are access problems, as to be expected. Windows 7 is a lot more robust, so directories are better protected.

Solve the access problems

  1. Run this as administrator.
  2. Open the properties on c:/program files/delphixxx directory.
  3. On the first tab Clear the Read-only attribute and press Apply for all files including subdirectory.
  4. Open the security tab
  5. Give users (and admins if necessary) full access to c:/program files (X86)/delphixxx directory

Now Delphi can be used to write programs again.

Edit August 2010: Windows 7 64 bit is also not a problem. Delphi is installed in C:\PROGRAM Files (X86)

Fix Winhelp

Help does not function, a windows pops up telling “winhelp is not supported anymore on this Windows version, go to microsoft.com for a fix” .

Search  Microsoft.com Windows Help program for Windows 7′ or 8 or 8.1 on microsoft.com, currently here  install that.

Now on Windows 8.1 (64 bit) I got the winhelp installer throwing ‘Incompatible sytem’ errors at me. After some research it was solved by downloading installing the English (US) language pack and have that active during the install (Charms, PC Settings etc).

Now help functions also.

I have upgraded my Windows 8.1 64 bit and Delphi 7 continued to work. So far so good. It means the procedure to get Delphi 7 on Windows 10 is still OK.

What did no work? The Help function is broken again, Microsoft did not (yet?) update he HELP support for Windows 10.
Read More