2 - Forth Interest Group
2 - Forth Interest Group
2 - Forth Interest Group
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Forth</strong> in Control<br />
Ken Merk<br />
Langley, British Columbia, Canada<br />
The following article is my attempt to contribute back<br />
to the <strong>Forth</strong> community some of my experiences in solving<br />
hardware interfacing problems using <strong>Forth</strong>. My formal<br />
training is in hardware, so I consider myself a beginner<br />
when it comes to <strong>Forth</strong> or any software-related projects.<br />
I found <strong>Forth</strong> a powerful tool to interface the computer to<br />
the outside world. Its speed and interactive qualities let<br />
you build "modules" that can be tried out with immediate<br />
feedback from your hardware. Once a solid foundation of<br />
primitive words is established, building up from there<br />
goes quickly.<br />
The <strong>Forth</strong> package I use is F-PC by Tom Zimmer,<br />
which I originally adopted to learn 8086 assembler language.<br />
My first try at an application using F-PC was very<br />
successful. Using SMENU.SEQ as a foundation for pulldown<br />
menus (with mouse interface), and words like<br />
BOXLFILL, SAVESCR, and RESTSCR for pop-up windows,<br />
LINEEDITOR for text input with full edit capabili-<br />
ties, FUNKEY. SEQ for function-key input, and~CoL0R. SEQ<br />
to make it pretty, it turned out to be a professional-looking<br />
program. I thought to myself-just think what I could do<br />
if I knew what I was doing!<br />
Using the PC, a <strong>Forth</strong> disk, and a few electronic<br />
components, we can build a simple interface to control<br />
devices in the real world. The first step is to get some I/O<br />
lines out of the computer. There are four basic routes we<br />
can go:<br />
1. Use the expansion slots located on the computer's<br />
motherboard. To do this, we need to build an address<br />
decoder to select some specific I/O port space, and<br />
some latches or PIA chips to get our I/O off the data bus<br />
into the outside world. There is 1K of port addresses<br />
available here, enough for any situation we can think of.<br />
This is the most versatile and complex to implement.<br />
2. We could use the RS-232 port to get control data in and<br />
out of the computer. This again involves building some<br />
electronics into our interface. We will need a serial-toparallel<br />
converter to change the serial bitstream into<br />
useful parallel control data. A UART and baud rate<br />
generator would work for this application.<br />
3. The PC game port is another pathway into your<br />
computer. We can sense four digital inputs and four<br />
analog inputs. There is no provision for data output, so<br />
it limits us to just receiving data from the outside world.<br />
4. The parallel printer port is the easiest and least com-<br />
plex to implement. We can bring eight outputs to the<br />
outside world for control purposes. The port is also bi-<br />
directional, so we can implement some input lines to<br />
receive data.<br />
To keep this project as simple as possible, we will use<br />
the parallel printer port and build an interface to control<br />
eight devices. For now, the eight devices will be LEDs<br />
(light-emitting diodes), which will represent the odoff<br />
state of each bit on the port.<br />
The first thing we need to do is determine what port<br />
address is assigned to your parallel printer card. The three<br />
possible parallel ports referred to as LPTI, LPT2, and LPT3<br />
are supported by three base addresses. At boot up, the<br />
BIOS searches for parallel ports at each of the three base<br />
addresses. The search is always performed in a specific<br />
order.<br />
1. Location 03BC (hex) is polled first. A byte is written to<br />
address 03BC and then read back to see if it matches<br />
what was sent.<br />
2. Location 0378 (hex) is polled second.<br />
3. Location 0278 (hex) is polled last.<br />
The first port that is found is assigned the name LPTI, the<br />
next one is assigned LPT2, and the last one is LPT3.<br />
When you first turn on your computer, the BIOS<br />
displays an information screen which tells you the ad-<br />
dresses of your parallel ports and LPT assignments. If your<br />
BIOS does not support this feature, you can use the System<br />
Information utility in PC TOOLS, under the I/O Port<br />
heading, to obtain this information. When all else fails, we<br />
can use <strong>Forth</strong> to get this information. On boot up, the<br />
address of the first parallel port found is stored in address<br />
locations 0040:0008 and 0040:0009. To view it, we can use<br />
<strong>Forth</strong>'s dump routine.<br />
HEX 0040 0008 10 LDUMP<br />
A chart will be displayed showing 16 consecutive address<br />
1 locations, starting from location 0040:0008. Address loca-<br />
~<br />
July 7 995 August 18 <strong>Forth</strong> Dimensions