280 CHAPTER 8
8–4 DISK FILES
Data are found stored on the disk in the form of files. The disk itself is organized in four main
parts: the boot sector, the file allocation table (FAT), the root directory, and the data storage
areas. The Windows NTFS (New Technology File System) contains a boot sector and a master
file table (MFT). The first sector on the disk is the boot sector, which is used to load the disk
operating system (DOS) from the disk into the memory when power is applied to the computer.
The FAT (or MFT) is where the names of files/subdirectories and their locations on the disk
are stored by the operating system. All references to any disk file are handled through
the FAT (or MFT). All other subdirectories and files are referenced through the root directory in
the FAT system. The NTFS system does not have a root directory even though the file system may
still appear to have a root directory. The disk files are all considered sequential access files, mean-
ing that they are accessed a byte at a time, from the beginning of the file toward the end. Both the
NTFS file system and the FAT file system are in use, with the hard disk drive on most modern
Windows systems using NTFS and the floppy disk, CD-ROM, and DVD using the FAT system.
Disk Organization
Figure 8–7 illustrates the organization of sectors and tracks on the surface of the disk. This orga-
nization applies to both floppy and hard disk memory systems. The outer track is always track 0,
and the inner track is 39 (double density) or 79 (high density) on floppy disks. The inner track on
a hard disk is determined by the disk size, and could be 10,000 or higher for very large hard disks.
Figure 8–8 shows the organization of data on a disk. The length of the FAT is determined
by the size of the disk. In the NTFS system, the length of the MFT is determined by the number
Sector Track 0
Index hole
Inner track
Drive hub
FIGURE 8–7 Structure of the disk.
PROGRAMMING THE MICROPROCESSOR 281
FIGURE 8–8 Main Boot
data storage areas on
a disk. MFT in NTFS
FAT Root Files and other directories
Track 0
Sector 0
of files stored on the disk. Likewise, the length of the root directory, in a FAT volume, is deter-
mined by the number of files and subdirectories located within it. The boot sector is always a sin-
gle 512-byte-long sector located in the outer track at sector 0, the first sector.
The boot sector contains a bootstrap loader program that is read into RAM when the sys-
tem is powered. The bootstrap loader then executes and loads the operating system into RAM.
Next, the bootstrap loader passes control to the operating system program, allowing the com-
puter to be under the control of and execute Windows, in most cases. This same sequence of
events also occurs if the Linux operating system is found on the disk.
The FAT indicates which sectors are free, which are corrupted (unusable), and which con-
tain data. The FAT table is referenced each time that the operating system writes data to the disk
so that it can find a free sector. Each free cluster is indicated by 0000H in the FAT and each occu-
pied sector is indicated by the cluster number. A cluster can be anything from one sector to any
number of sectors in length. Many hard disk memory systems use four sectors per cluster, which
means that the smallest file is 512 bytes × 4, or 2048 bytes long. In a system that uses NTFS, the
cluster size is usually 4K bytes, which is eight sectors long.
Figure 8–9 shows the format of each directory entry in the root, or in any other directory or
subdirectory. Each entry contains the name, extension, attribute, time, date, location, and length.
The length of the file is stored as a 32-bit number. This means that a file can have a maximum
length of 4G bytes. The location is the starting cluster number.
Windows NTFS uses a much larger directory entry or record (1,024 bytes) than that of
the FAT system (32 bytes). The MFT record contains the file name, file date, attribute, and
data. The data can be the entire contents of the file, or a pointer to where the data is stored on
the disk called a file run. Generally files that are smaller than about 1500 bytes fit into the
MFT record. Longer files fit into a file run or file runs. A file run is a series of contiguous
clusters that store the file data. Figure 8–10 illustrates an MFT record in the Windows NTFS
file system. The information attribute contains the create date, last modification date, create
time, last modification time, and file attributes such as read-only, archive, and so forth. The
security attribute stores all security information for the file for limiting access to the file in the
Windows system. The header stores information about the record type, size, name (optional),
and whether it is resident or not.
File Names
Files and programs are stored on a disk and referenced both by a file name and an extension
to the file name. With the DOS operating system, the file name may only be from one to
eight characters long. The file name contains just about any ASCII character, except for spaces
or the “ \ . / [ ] * , : < > I ; ? = characters. In addition to the file name, the file can have an optional
282 CHAPTER 8
32-byte directory entry 8 4 2 1 16 8 4 2 1
Length (high order) Year* Month Day
1E (2 bytes)
16 8 4 2 1 32 16 8 4 2 1 X X X X X
Length (low order)
1C (2 bytes) Hours Minutes Unused
File cluster location 17 16
1A (2 bytes)
0 0 A DV S H R A = Archive
18 Date D = Subdirectory
16 Time V = Volume label
15 Unused S = System file
C (10 bytes) H = Hidden file
R = Read-only
B Attribute
*Note: year 8 = 1988, year 9 = 1989, year 10 = 1990, etc.
A Extension
8 (3 bytes)
7 Name
(8 bytes)
0
FIGURE 8–9 Format of any FAT directory or subdirectory entry.
one- to three-digit extension to the file name. Note that the name of a file and its extension are
always separated by a period. If Windows 95 through Windows XP is in use, the file name can be
of any length (up to 255 characters) and can even contain spaces. This is an improvement over
the eight-character file name limitation of DOS. Also note that a Windows file can have more
than one extension.
Directory and Subdirectory Names. The DOS file management system arranges the data
and programs on a disk into directories and subdirectories. In Windows directories and sub-
directories are called file folders. The rules that apply to file names also apply to file folder
names. The disk is structured so that it contains a root directory when first formatted. The root
directory or folder for a hard disk used as drive C is C:\. Any other folder is placed in the root
directory. For example, C:\DATA is folder DATA in the root directory. Each folder placed in
the root directory can also have subdirectories or subfolders. Examples are the subfolders
C:\DATA\AREA1 and C:\DATA\AREA2, in which the folder DATA contains two subfolders:
AREA1 and AREA2. Subfolders can also have additional subfolders. For example,
C:\DATA\AREA2\LIST depicts folder DATA, subfolder AREA, which contains a subfolder
called LIST.
Sequential Access Files
All DOS files and Windows files are sequential files. A sequential file is stored and accessed
from the beginning of the file toward the end, with the first byte and all bytes between it and the
last accessed to read the last byte. Fortunately, files are read and written in C++ using the File
class, which makes their access and manipulation easy. This section of the text describes how to
FIGURE 8–10 A record
in the Master File Table in
the NTFS system.
PROGRAMMING THE MICROPROCESSOR 283
create, read, write, delete, and rename a sequential access file. To gain access to the File class, a
new using must be added to the list of using statements at the top of the program. If file access is
needed, add a using namespace System::IO; statement to the program.
File Creation. Before a file can be used, it must exist on the disk. A file is created by the File
class using Create as an attribute that directs File to create a file. A file is created with create as
illustrated in Example 8–29. Here the name of the file that is created by the program is stored in
a Stringˆ called FileName. Next, the File class is used to test and see if the file already exists
before creating it. Finally, in the if statement the file is created.
In this example, if the file fails to open because the disk is full or the folder is not found,
a Windows message box displays Cannot create file followed by the file name, and an exit
from the program occurs when OK is clicked in the message box. To try this example, create a
dialog application and place the code in the Load event handler. Choose a folder name that
does not exist (test should probably work) and run the application. You should see the error
message. If you change the FileName so it does not include the folder, you will not get the
error message.
EXAMPLE 8–29
Stringˆ fileName = “C:\\Test.txt”;
if (File::Exists(fileName) == false)
{ // don’t forget using namespace System::IO;
try
{
File::Create(fileName);
}
catch (...)
{
MessageBox::Show(“Cannot create ” + fileName);
Application::Exit();
}
}
// Test.txt now exists with a length of 0 bytes
Writing to a File. Once a file exists, it can be written to. In fact, it would be highly unusual to
create a file without writing something to it. Data are written to a file one byte at a time. The
FileStream class is used to write a stream of data to the file. Data are always written starting at
the very first byte in a file. Example 8–30 lists a program that creates a file in the root directory
called Test1.txt and stores the letter A in each of its 256 bytes. If you execute this code and look
at Test1.txt with NotePad, you will see a file filled with 256 letter As. Note that the file stream
should be closed when finished using Close( ) function. Also notice in this example that an array
of size byte is created using the garbage collection class in C++. It is important to use this class
to create a managed array of data.
EXAMPLE 8–30
Stringˆ fileName = “C:\\Test1.txt”;
array<Byte>ˆ buffer = gcnew array<Byte>(256);
try
{
FileStreamˆ fs = File::OpenWrite(fileName);
for (int a = 0; a < 256; a++)
{
buffer[a] = ‘A’;
}
284 CHAPTER 8
fs->Write(buffer, 0, buffer->Length);
fs->Close();
}
catch (...)
{
MessageBox::Show(“Disk error”);
Application::Exit();
}
Suppose that a 32-bit integer must be written to a file. Because only bytes can be written, a
method must be used to convert the four bytes of the integer into a form that can be written to a
file. In C++ shifts are used to place the byte into the proper location to store in the array.
Assembly language can also accomplish the same task in fewer bytes, as listed in Example 8–31.
If you look at the assembly code for each method, you see that the assembly language method is
much shorter and much faster. If speed and size are important, then the assembly code is by far
the best choice, although in this case the code generated by C++ is fairly efficient.
EXAMPLE 8–31
int number = 0x20000;
array<Byte>ˆ buf = gcnew array<Byte>(4);
//C++ conversion
buf[0] = number;
buf[1] = number >> 8;
buf[2] = number >> 16;
buf[3] = number >> 24;
//Assembly language conversion
_asm
{
mov eax,number
mov buf[0],al
mov buf[1],ah
bswap eax ;little endian to big endian
mov buf[2],ah
mov buf[3],al
}
Reading File Data. File data are read from the beginning of the file toward the end using the
OpenRead member of File. Example 8–32 shows an example that reads the file written in
Example 8–30 into a buffer called buffer1. The OpenRead function returns the number of bytes
actually read from the file, but not used in this example. This works fine if the size of the file is
known as it is here, but suppose that the length of the file is not known. The FileInfo class is used
to find the length of a file as illustrated in Example 8–33.
EXAMPLE 8–32
Stringˆ fileName = “C:\\Test1.txt”;
array<Byte>ˆ buffer1 = gcnew array<Byte>(256);
try
{
FileStreamˆ fs = File::OpenRead(fileName);
fs->Read(buffer1, 0, 256);
fs->Close();
}
catch (...)
{
MessageBox::Show(“Disk error”);
Application::Exit();
}
EXAMPLE 8–33
Stringˆ fileName = “C:\\Test1.txt”;
PROGRAMMING THE MICROPROCESSOR 285
FIGURE 8–11 The HexDump program.
FileInfoˆ fi = gcnew FileInfo(fileName);
int fileLength = fi->Length;
An Example Binary Dump Program. One tool not available with Windows is a program that
displays the contents of a file in hexadecimal code. Although this may not be used by most pro-
grammers, it is used whenever software is developed so that the actual contents of a file can be
viewed in hexadecimal format. Start a forms application in Windows and call it HexDump. Place
a control called a Rich Textbox onto the form as illustrated in Figure 8–11. Under Properties for
the Rich Textbox Control, make sure you change Locked to true and Scroll bars to Vertical. If
you display a very large file, you will want to be able to scroll down through the code. Very large
files take some time to load in this program.
This program uses the function (Disph) shown earlier in Example 8–24 to display the address
as an eight-digit hexadecimal address and also to display the contents of the address in hexadecimal
form as a two-digit number. Add Disph function to the program so it returns a String at the location
addressed by char temp as the third parameter. The first two parameters contain two integers: one
for the number and one for the number of digits called size, as shown in Example 8–34.
Example 8–34 shows the entire program required to perform a hexadecimal dump. Most of
the program is generated by Visual C++, only the function at the top and a few at the end were
entered to create the application. Note that to change the file for this program requires a change
of the name of the file in the program. This can be modified by using an edit box to enter the file
name, but it was not done in this example for sake of brevity. In this program 16 bytes are read at
a time and formatted for display. This process continues until no bytes remain in the file. The
286 CHAPTER 8
ASCII data that are displayed at the end of the hexadecimal listing are filtered so that any ASCII
character under 32 (a space) are displayed as a period. This is important or control characters
such as line feed, backspace, and the like will destroy the screen formatting of the ASCII text,
and that is undesirable.
EXAMPLE 8–34
#pragma once
namespace HexDump1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
// assembly code here compiled using the /CLR switch
void Disph(unsigned int number, unsigned int size, char* temp)
{
int a;
number <= ( 8 - size ) * 4; //adjust position
for (a = 0; a < size; a++)
{
char temp1;
_asm
{
rol number, 4;
mov al,byte ptr number
and al,0fh ;make 0 - f
add al,30h ;convert to ASCII
cmp al,39h
jbe Disph1
add al,7
Disph1:
mov temp1,al
}
temp[a] = temp1; //add digit to string
}
temp[a] = 0; // null string end
}
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to
change the
/// ‘Resource File Name’ property for the managed resource
compiler tool
/// associated with all .resx files this class depends on.
Otherwise,
/// the designers will not be able to interact properly with
localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
PROGRAMMING THE MICROPROCESSOR 287
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::RichTextBoxˆ richTextBox1;
private: System::Windows::Forms::OpenFileDialogˆ openFileDialog1;
private: System::Windows::Forms::Buttonˆ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ˆcomponents;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->richTextBox1 = (gcnew
System::Windows::Forms::RichTextBox());
this->openFileDialog1 = (gcnew
System::Windows::Forms::OpenFileDialog());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// richTextBox1
//
this->richTextBox1->Font = (gcnew
System::Drawing::Font(L“Courier New”, 9.75F,
System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->richTextBox1->Location = System::Drawing::Point(12,
12);
this->richTextBox1->Name = L“richTextBox1”;
this->richTextBox1->ScrollBars =
System::Windows::Forms::RichTextBoxScrollBars::Vertical;
this->richTextBox1->Size = System::Drawing::Size(657, 420);
this->richTextBox1->TabIndex = 0;
this->richTextBox1->Text = L“”;
//
// openFileDialog1
//
this->openFileDialog1->FileName = L“openFileDialog1”;
//
// button1
//
this->button1->Location = System::Drawing::Point(601, 438);
this->button1->Name = L“button1”;
this->button1->Size = System::Drawing::Size(68, 25);
this->button1->TabIndex = 1;
this->button1->Text = L“Open”;
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
288 CHAPTER 8
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(681, 468);
this->Controls->Add(this->button1);
this->Controls->Add(this->richTextBox1);
this->Name = L“Form1”;
this->ShowIcon = false;
this->StartPosition =
System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L“HexDump”;
this->ResumeLayout(false);
}
#pragma endregion
private: System::Stringˆ Disp(int number, int size)
{
char temp[9];
Disph(number,size,temp);
Stringˆ a = ““;
int count = 0;
while (temp[count] != 0) // convert to string
{
Char b = temp[count++];
a += b;
}
return a;
}
private: System::Void button1_Click(System::Objectˆ sender,
System::EventArgsˆ e)
{
array<Byte>ˆ buffer = gcnew array<Byte>(1000000);
int fileLength;
Stringˆ line = “”;
if (openFileDialog1->ShowDialog() ==
System::Windows::Forms::DialogResult::OK)
{
try
{
FileStreamˆ fs = File::OpenRead(openFileDialog1->FileName);
fs->Read(buffer, 0, 1000000);
fs->Close();
FileInfoˆ fi = gcnew FileInfo(openFileDialog1->FileName);
fileLength = fi->Length;
this->Text = “HexDump -- ” + openFileDialog1->FileName;
}
catch (...)
{
MessageBox::Show(“Disk error”);
Application::Exit();
}
for (int a = 0; a < fileLength; a++)
{
if (a % 16 == 0)
{
if (a != 0)
{
richTextBox1->Text += “ ” + line;
line = “”;
richTextBox1->Text += “\n”;
}
richTextBox1->Text += Disp(a, 8);
}
richTextBox1->Text += “ ” + Disp(buffer[a], 2);
if (buffer[a] >= 32 && buffer[a] < 128)
line += Convert::ToChar(buffer[a]);
else
line += “.”;
PROGRAMMING THE MICROPROCESSOR 289
}
richTextBox1->Text += “ ” + line;
}
else
{
this->Text = “HexDump”;
}
}
};
}
The File Pointer and Seek. When a file is opened, written, or read, the file pointer addresses
the current location in the sequential file. When a file is opened, the file pointer always addresses
the first byte of the file. If a file is 1024 bytes long, and a read function reads 1023 bytes, the file
pointer addresses the last byte of the file, but not the end of the file.
The file pointer is a 32-bit number that addresses any byte in a file. The File Append
member function is used to add new information to the end of a file. The file pointer can be
moved from the start of the file or from the end of the file. Open moves the pointer to the start of
the file. In practice, both are used to access different parts of the file. The FileStream member
function Seek allows the file pointer to be moved to the start of a file (SeekOrigin::Begin), the
end of a file (SeekOrigin::End), or the current location in the file (SeekOrigin::Current). The first
number in the Seek function is the offset. If the third byte in the file is accessed, it is accessed
with a Seek(2, SeekOrigin::Begin) function. (The third byte is at offset 2.) Note that the second
number in the Write function is also an offset and can be used in the same manner as a Seek.
Suppose that a file exists on the disk and that you must append the file with 256 bytes of
new information. When the file is opened, the file pointer addresses the first byte of the file. If
you attempt to write without moving the file pointer to the end of the file, the new data will
overwrite the first 256 bytes of the file. Example 8–35 shows a sequence of instructions for
Appends, which adds 256 bytes of data to the end of the file, and then closes the file. This file is
appended with 256 new bytes of data from area Buffer.
EXAMPLE 8–35
Stringˆ fileName = “C:\\Test1.txt”;
array<Byte>ˆ buffer = gcnew array<Byte>(256);
try
{
FileStreamˆ fs = File::OpenWrite(fileName);
for (int a = 0; a < 256; a++)
{
buffer[a] = ‘S’;
}
fs->Seek(0, SeekOrigin::End);
fs->Write(buffer, 0, buffer->Length);
fs->Close();
}
catch (...)
{
MessageBox::Show(“Disk error”);
Application::Exit();
}
// or the same operation is performed using the offset number
// in the Write function as follows:
Stringˆ fileName = “C:\\Test1.txt”;
array<Byte>ˆ buffer = gcnew array<Byte>(256);
290 CHAPTER 8 Old file New file
Old file
FIGURE 8–12 Inserting
new data within an old file. Insert data
Insert point Insert data
Old file
try
{
FileStreamˆ fs = File::OpenWrite(fileName);
for (int a = 0; a < 256; a++)
{
buffer[a] = ‘S’;
}
fs->Write(buffer, 256, buffer->Length);
fs->Close();
}
catch (...)
{
MessageBox::Show(“Disk error”);
Application::Exit();
}
One of the more difficult file maneuvers is inserting new data in the middle of the file.
Figure 8–12 shows how this is accomplished by creating a second file. Notice that the part of the
file before the insertion point is copied into the new file. This is followed by the new information
before the remainder of the file is appended after the insertion in the new file. Once the new file
is complete, the old file is deleted and the new file is renamed to the old file name.
Example 8–36 shows a program that inserts new data into an old file. This program copies
the Data.new file into the Data.old file at a point after the first 256 bytes of the Data.old file. The
new data from buffer2 is next added to the file and then this is followed by the remainder of the
old file. New File member functions are used to delete the old file and rename the new file to the
old file name.
EXAMPLE 8–36
private: System::Void Form1_Load(System::Objectˆ sender,
System::EventArgsˆ e)
{
Stringˆ fileName1 = “C:\\Data.old”;
Stringˆ fileName2 = “C:\\Data.new”;
int fileLength;
array<Byte>ˆ buffer1 = gcnew array<Byte>(256);
array<Byte>ˆ buffer2 = gcnew array<Byte>(6);
try
{
FileStreamˆ fs1 = File::OpenWrite(fileName1);
FileStreamˆ fs2 = File::OpenWrite(fileName2);
PROGRAMMING THE MICROPROCESSOR 291
FileInfoˆ fi = gcnew FileInfo(fileName1);
fileLength = fi->Length;
fs1->Read(buffer1, 0, 256);
fs2->Write(buffer1, 0, 256);
fs2->Write(buffer2, 0, 6);
fileLength -= 256;
while (fileLength > 0)
{
fs1->Read(buffer1, 0, 256);
fs2->Write(buffer1, 0, 256);
fileLength -= 256;
}
fs1->Close();
fs2->Close();
}
catch (...)
{
MessageBox::Show(“Disk error”);
Application::Exit();
}
}
Random Access Files
Random access files are developed through software using sequential access files. A random
access file is addressed by a record number rather than by going through the file searching for
data. The Seek function becomes very important when random access files are created. Random
access files are much easier to use for large volumes of data, which are often called databases.
Creating a Random Access File. Planning is paramount when creating a random access file
system. Suppose that a random access file is required for storing the names of customers. Each
customer record requires 32 bytes for the last name, 32 bytes for the first name, and one byte for
the middle initial. Each customer record contains two street address lines of 64 bytes each, a city
line of 32 bytes, two bytes for the state code, and nine bytes for the Zip Code. The basic customer
information alone requires 236 bytes; additional information expands the record to 512 bytes.
Because the business is growing, provisions are made for 5000 customers. This means that the
total random access file is 2,560,000 bytes long.
Example 8–37 illustrates a short program that creates a file called CUST.FIL and inserts
5000 blank records of 512 bytes each. A blank record contains 00H in each byte. This appears be
a large file, but it fits on the smallest of hard disks.
EXAMPLE 8–37
private: System::Void Form1_Load(System::Objectˆ sender,
System::EventArgsˆ e)
{
Stringˆ fileName = “C:\\Cust.fil”;
array<Byte>ˆ buffer = gcnew array<Byte>(512);
for ( int a = 0; a < 512; a++ ) //fill buffer
{
buffer[a] = 0;
}
try
{
FileStreamˆ fs = File::OpenWrite(fileName);
for (int a = 0; a < 5000; a++)
{
fs->Write(buffer, 0, 512);
}
fs->Close();
292 CHAPTER 8
}
catch (...)
{
MessageBox::Show(“Disk error”);
Application::Exit();
}
}
Reading and Writing a Record. Whenever a record must be read, the record number is found by
using a Seek. Example 8–38 lists a function that is used to Seek to a record. This function assumes
that a file has been opened as CustomerFile and that the CUST.FIL remains open at all times.
Notice how the record number is multiplied by 512 to obtain a count to move the file pointer
using a Seek. In each case, the file pointer is moved from the start of the file to the desired record.
EXAMPLE 8–38
void CCusDatabaseDlg::FindRecord(unsigned int RecordNumber)
{
File.Seek( RecordNumber * 512, CFile::begin );
}
Other functions (listed in Example 8–39) are needed to manage the customer database.
These include WriteRecord, ReadRecord, FindLastNameRecord, FindBlankRecord, and so on.
Some of these are listed in the example as well as the data structure that contains the information
for each record.
EXAMPLE 8–39
// class placed before the form1 class for containing a record
public ref class Customer
{
public: static array<Byte>ˆ FirstName = gcnew array<Byte>(32);
public: static array<Byte>ˆ Mi = gcnew array<Byte>(1);
public: static array<Byte>ˆ LastName = gcnew array<Byte>(32);
public: static array<Byte>ˆ Street1 = gcnew array<Byte>(64);
public: static array<Byte>ˆ Street2 = gcnew array<Byte>(64);
public: static array<Byte>ˆ City = gcnew array<Byte>(32);
public: static array<Byte>ˆ State = gcnew array<Byte>(2);
public: static array<Byte>ˆ ZipCode = gcnew array<Byte>(9);
public: static array<Byte>ˆ Other = gcnew array<Byte>(276);
};
// functions placed at the end of the form1 class
static array<Byte>ˆ buffer = gcnew array<Byte>(512);
static Stringˆ fileName = “C:\\Cust.fil”;
static FileStreamˆ fs;
static Customer Record;
private: System::Void Form1_Load(System::Objectˆ sender,
System::EventArgsˆ e)
{ // open the file when the application starts
Customer Record;
try
{
fs = File::OpenWrite(fileName);
for (int a = 0; a < 5000; a++)
{
fs->Write(buffer, 0, 512);
}
fs->Close();
}
catch (...)
{
PROGRAMMING THE MICROPROCESSOR 293
MessageBox::Show(“Disk error”);
Application::Exit();
}
}
private: System::Void FindRecord(unsigned int RecordNumber)
{
fs->Seek(RecordNumber * 512, SeekOrigin::Begin);
}
private: System::Void WriteRecord(unsigned int RecordNumber)
{
FindRecord(RecordNumber);
fs->Write(Record.FirstName, 0, 32);
fs->Write(Record.Mi, 0, 1);
fs->Write(Record.LastName, 0, 32);
fs->Write(Record.Street1, 0, 64);
fs->Write(Record.Street2, 0, 64);
fs->Write(Record.City, 0, 32);
fs->Write(Record.State, 0, 2);
fs->Write(Record.ZipCode, 0, 9);
}
private: System::Void ReadRecord(unsigned int RecordNumber)
{
FindRecord(RecordNumber);
fs->Read(Record.FirstName, 0, 32);
fs->Read(Record.Mi, 0, 1);
fs->Read(Record.LastName, 0, 32);
fs->Read(Record.Street1, 0, 64);
fs->Read(Record.Street2, 0, 64);
fs->Read(Record.City, 0, 32);
fs->Read(Record.State, 0, 2);
fs->Read(Record.ZipCode, 0, 9);
}
private: System::UInt32 FindFirstName(array<Byte>ˆ FirstName)
{
for ( int a = 0; a < 5000; a++ )
{
ReadRecord(a);
if (Record.FirstName == FirstName)
{
return a; //if found return record number
}
}
return 5001; //if not found return 5001
}
private: System::UInt32 FindBlankRecord()
{
for ( int a = 0; a < 5000; a++ )
{
ReadRecord(a);
if (Record.LastName[0] == 0 )
{
return a;
}
}
return 0;
}
294 CHAPTER 8
FIGURE 8–13 The
DataTime application.
8–5 EXAMPLE PROGRAMS
Now that many of the basic programming building blocks have been discussed, we present
some example application programs. Although these example programs may seem trivial,
they show some additional programming techniques and illustrate programming styles for the
microprocessor.
Time/Date Display Program
Although this program does not use assembly language, it does demonstrate how to obtain the
date and time from the Windows API and how to format it for display. It also illustrates how to
use a timer in Visual C++. Example 8–40 illustrates a program that uses a timer, set to interrupt
the program once per second, to display the time and date. The time and date are obtained by
using DateTime object to read the computer time and date into a variable called dt. The format
member TimeDate is used to format the dt variable. Create a dialog application called DateTime
and place two labels on it as shown in Figure 8–13.
EXAMPLE 8–40
private: System::Void Form1_Load(System::Objectˆ sender,
System::EventArgsˆ e)
{
ShowDateTime();
}
private: System::Void ShowDateTime()
{
DateTime dt = DateTime::Now; // get current time
label1->Text = dt.Hour.ToString() + “:” + dt.Minute.ToString();
label2->Text = dt.Date.ToLongDateString();
}
PROGRAMMING THE MICROPROCESSOR 295
FIGURE 8–14 A bubble sort
showing data as they are
sorted. Note: Sorting five
numbers may require four
passes.
private: System::Void timer1_Tick(System::Objectˆ sender,
System::EventArgsˆ e)
{
ShowDateTime();
}
Numeric Sort Program
At times, numbers must be sorted into numeric order. This is often accomplished with a bubble
sort. Figure 8–14 shows five numbers that are sorted with a bubble sort. Notice that the set of five
numbers is tested four times with four passes. For each pass, two consecutive numbers are com-
pared and sometimes exchanged. Also notice that during the first pass there are four compar-
isons, during the second three, and so forth.
Example 8–41 illustrates a program that accepts 10 numbers from the keyboard (32-bit
integers). After these 32-bit numbers are accepted and stored in memory section numbers, they
are sorted by using the bubble-sorting technique. This bubble sort uses a swap flag to determine
whether any numbers were exchanged in a pass. If no numbers were exchanged, the numbers are
in order and the sort terminates. This early termination normally increases the efficiency of the
sort because numbers are rarely completely out of order.
Once the numbers are sorted, they are displayed in ascending order. Figure 8–15 shows
how the application appears after it is executed.
FIGURE 8–15 The bubble
sort.
296 CHAPTER 8
EXAMPLE 8–41
void Sort(int* data)
{
char flag;
_asm
{
mov ecx,9 ;9 for 10 numbers
L1:
mov flag,0 ;clear flag
mov edx,0
L2:
mov ebx,data
mov eax,[ebx+edx*4]
cmp eax,[ebx+edx*4+4]
jbe L3
push eax ;swap
mov eax,[ebx+edx*4+4]
mov [ebx+edx*4], eax
pop dword ptr [ebx+edx*4+4]
mov flag,1 ;set flag
L3:
inc edx
cmp edx,ecx
jne L2
cmp flag,0
jz L4 ;if no swaps
loop L1
L4:
}
}
bool isHandled;
private: System::Void button1_Click(System::Objectˆ sender,
System::EventArgsˆ e)
{
int numbers[10];
int count = 0;
int digit = 0;
int a;
for(a = 0; a < 10; a++)
{
numbers[a] = 0;
while (digit < textBox1->Text->Length && textBox1->Text[digit]
!= ‘,’)
{
numbers[a] = numbers[a] * 10 +
(int)(textBox1->Text[digit] - 0x30);
digit++;
}
digit++;
if (digit >= textBox1->Text->Length)
{
break;
}
}
if (a == 9)
{
Sort(numbers);
label2->Text = “”;
for (int a = 0; a < 9; a++)
{
label2->Text += numbers[a].ToString() + “, ”;
}
PROGRAMMING THE MICROPROCESSOR 297
label2->Text += numbers[9].ToString();
}
else
{
MessageBox::Show(
“10 numbers must be entered separated by commas”);
}
}
private: System::Void textBox1_KeyDown(System::Objectˆ sender,
System::Windows::Forms::KeyEventArgsˆ e)
{
isHandled = true;
if (e->KeyCode >= Keys::D0 && e->KeyCode <= Keys::D9 ||
e->KeyCode == Keys::Oemcomma || e->KeyCode == Keys::Back)
{
isHandled = false;
}
}
private: System::Void textBox1_KeyPress(System::Objectˆ sender,
System::Windows::Forms::KeyPressEventArgsˆ e)
{
e->Handled = isHandled;
}
Data Encryption
Data encryption seems to be the vogue at this time because of the security aspect of many sys-
tems. To illustrate simple data encryption for a character string, suppose that each character in a
string is exclusive-ORed with a number called an encryption key. This certainly changes the code
of the character, but to make it a bit more random, suppose that the encryption key is changed
after each character is encrypted. In this way patterns are much harder to detect in the encrypted
message, making it harder to decipher.
To illustrate this simple scheme, Figure 8–16 shows a screen shot of the program to test the
scheme, using a textbox control to accept a character string and a label to display the encrypted
message. This example was generated using an initial encryption key of 0×45. If the initial value
is changed, the encrypted message will change.
Example 8–42 lists the program used to generate the message in its encrypted form in a
rich textbox control. The button event handler reads the contents of the textbox control, used for
entering the character string to be encrypted, and uses a short assembly language function to
encrypt the string. Notice how the program uses assembly language to Exclusive-OR each char-
acter of the string with the EncryptionKey and then how the EncryptionKey is modified for the
next character. The technique used here increments the Encryption key and prevents the key from
becoming larger than 7FH. This technique can be made more intricate to make it even more dif-
ficult to decipher. For example, suppose that the key is incremented on every other character and
that is alternated with inverting the key, as shown in Example 8–43. Almost any combination of
operations can be used to modify the key between passes to make it very difficult to decode. In
practice we use a 128-bit key and the technique for modification is different, but nonetheless, this
is basically how encryption is performed. Because Example 8–40 uses an 8-bit key, the
encrypted message could be cracked by trying all 256 (28) possible keys, but if a 128-bit key is
used, it requires far many more attempts (2128) to crack—an almost impossible number of
attempts.
298 CHAPTER 8
FIGURE 8–16 Data encryp-
tion application.
EXAMPLE 8–42
char EncryptionKey = 0x45;
char Encrypt(char code)
{
_asm
{
mov al,code
xor al,EncryptionKey
mov code,al
mov al,EncryptionKey
inc al
and al,7fh
mov EncryptionKey,al
}
return code;
}
private: System::Void button1_Click(System::Objectˆ sender,
System::EventArgsˆ e)
{
richTextBox1->Text = “”;
for (int a = 0; a < textBox1->Text->Length; a++)
{
richTextBox1->Text += Convert::ToChar(Encrypt(textBox1->Text[a]));
}
}
EXAMPLE 8–43
//just the assembly language part of the program
PROGRAMMING THE MICROPROCESSOR 299
char EncryptionKey = 0x45;
char everyOther = 0;
char Encrypt(char code)
{
_asm
{
mov al,code
xor al,EncryptionKey
mov code,al
mov al,everyOther
inc al
and al,1
mov everyOther,al
mov bl,EncryptionKey
cmp al,0
jz L1
inc bl
jmp L2
L1:
not bl
L2:
and bl,7fh
mov EncryptionKey,bl
}
return code;
}
8–6 SUMMARY
1. The assembler program (ML.EXE) assembles modules that contain PUBLIC variables and
segments, plus EXTRN (external) variables. The linker program (LINK.EXE) links mod-
ules and library files to create a run-time program executed from the DOS command line.
The run-time program usually has the extension EXE, but might contain the extension
COM.
2. The MACRO and ENDM directives create a new opcode for use in programs. These macros
are similar to procedures, except that there is no call or return. In place of them, the assem-
bler inserts the code of the macro sequence into a program each time it is invoked. Macros
can include variables that pass information and data to the macro sequence.
3. Setting focus to an object is accomplished by using the Focus( ) member variable found with
most objects.
4. The Convert class in C++ is used to convert from one form to another in many cases, but not
in all cases.
5. The mouse driver is accessed from Windows by installing handlers for various Windows
events such as MouseMove, MouseDown, etc.
6. Conversion from binary to BCD is accomplished with the AAM instruction for numbers
that are less than 100 or by repeated division by 10 for larger numbers. Once the number is
converted to BCD, 30H is added to convert each digit to ASCII code for placement in a string.
7. When converting from an ASCII number to BCD, 30H is subtracted from each digit. To
obtain the binary equivalent, multiply by 10 and then add each new digit.
8. Lookup tables are used for code conversion with the XLAT instruction if the code is an 8-bit
code. If the code is wider than 8 bits, a short procedure that accesses a lookup table provides
the conversion. Lookup tables are also used to hold addresses so that different parts of a pro-
gram or different procedures can be selected.
9. Conditional assembly language statements allow portions of a program to be assembled
if a condition is met. These are useful for tailoring software to an application. In Visual
300 CHAPTER 8
C++ Express, a program that contains assembly code must be compiled with the /CLR
switch.
10. The disk, memory system contains tracks that hold information stored in sectors. Many disk sys-
tems store 512 bytes of information per sector. Data on the disk are organized in a boot sector, file
allocation table, root directory, and data storage area. The boot sector loads the DOS system from
the disk into the computer memory system. The FAT or MFT indicates which sectors are present
and whether they contain data. The root directory contains file names and subdirectories through
which all disk files are accessed. The data storage area contains all subdirectories and data files.
11. Files are manipulated with the File object in Visual C++. To read a disk file, the file must be
opened, read, and then closed. To write to a disk file, it must be opened, written, and then
closed. When a file is opened, the file pointer addresses the first byte of the file. To access
data at other locations, the file pointer is moved using a Seek before data are read or written.
12. A sequential access file is a file that is accessed sequentially from the beginning to the end.
A random access file is a file that is accessed at any point. Although all disk files are sequen-
tial, they can be treated as random access files by using software.
8–7 QUESTIONS AND PROBLEMS
1. The assembler converts a source file to a(n) _________ file.
2. What files are generated from the source file TEST.ASM if it is processed by ML.EXE?
3. The linker program links object files and _________ files to create an execution file.
4. What does the PUBLIC directive indicate when placed in a program module?
5. What does the EXTRN directive indicate when placed in a program module?
6. What directive appears with labels defined as external?
7. Describe how a library file works when it is linked to other object files by the linker program.
8. What assembler language directives delineate a macro sequence?
9. What is a macro sequence?
10. How are parameters transferred to a macro sequence?
11. Develop a macro called ADD32 that adds the 32-bit contents of DX-CX to the 32-bit con-
tents of BX-AX.
12. How is the LOCAL directive used within a macro sequence?
13. Develop a macro called ADDLIST PARA1,PARA2 that adds the contents of PARA1 to
PARA2. Each of these parameters represents an area of memory. The number of bytes added
are indicated by register CX before the macro is invoked.
14. Develop a macro that sums a list of byte-sized data invoked by the macro ADDM LIST,LENGTH.
The label LIST is the starting address of the data block and LENGTH is the number of data added.
The result must be a 16-bit sum found in AX at the end of the macro sequence.
15. What is the purpose of the INCLUDE directive?
16. Modify the function in Example 8–12 so that it filters the numbers 0 through 9 from only the
keyboard and not the keypad and ignores all other characters.
17. Modify the function in Example 8–12 so that it generates a random 8-bit number in class
variable char Random. (Hint: To accomplish this, increment Random each time that the
KeyDown function is called.)
18. Modify the software you developed in question 17 so that it generates a random number
between 9 and 62.
19. Modify the function listed in Example 8–15 so that the hexadecimal numbers use lowercase
letters a through f instead of the uppercase letters.
20. Modify Example 8–16 so it will shift/rotate left or right. This is accomplished by adding a
pair of radio buttons to select the direction.
PROGRAMMING THE MICROPROCESSOR 301
21. What event handlers are used to access the mouse in the Visual C++ programming environ-
ment and what event causes each handler to be called?
22. How is the right mouse button detected in a program?
23. How is a double-click detected with the mouse?
24. Develop software that detects when both the right and left mouse buttons are pressed
simultaneously.
25. How is a color selected in a program using Visual C++?
26. What is the purpose of the ForeColor property?
27. When a number is converted from binary to BCD, the _________ instruction accomplishes
the conversion, provided the number is less than 100 decimal.
28. How is a large number (over 100 decimal) converted from binary to BCD?
29. How could a binary number be displayed as an octal number?
30. A BCD digit is converted to ASCII code by adding a(n) _________.
31. An ASCII-coded number is converted to BCD by subtracting _________.
32. Develop a function that reads an ASCII number from a textbox control as keys are typed
(use KeyDown) on the keyboard and returns it as an unsigned int. The number in the textbox
is an octal number that is converted to binary by the function.
33. Explain how a three-digit ASCII-coded number is converted to binary.
34. Develop a function that converts all lowercase ASCII-coded letters into uppercase ASCII-
coded letters. Your procedure may not change any other character except the letters a–z and
must return the converted character as a char.
35. Develop a lookup table that converts hexadecimal data 00H–0FH into the ASCII-coded
characters that represent the hexadecimal digits. Make sure to show the lookup table and any
software required for the conversion. It is suggested that a function is created to perform the
conversion.
36. Explain the purpose of a boot sector, FAT, and root directory in the FAT system.
37. Explain the purpose of the MFT in the NTFS file system.
38. The surface of a disk is divided into tracks that are further subdivided into _________.
39. What is a bootstrap loader and where is it found?
40. What is a cluster?
41. The NTFS file system often uses cluster of _________ bytes in length.
42. What is the maximum length of a file?
43. What code is used to store the name of a file when long file names are in use?
44. DOS file names are at most _________ characters in length.
45. How many characters normally appear in an extension?
46. How many characters may appear in a long file name?
47. Develop a program that opens a file called TEST.LST, reads 512 bytes from the file into
memory area Array, and closes the file.
48. Show how to rename file TEST.LST to TEST.LIS.
49. What is the purpose of the File Move member function?
50. What is a control?
51. Write a program that reads any decimal number between 0 and 2G and displays the 32-bit
binary version on the video display.
52. Write a program that displays the binary powers of 2 (in decimal) on the video screen for the
powers 0 through 7. Your display shows 2n = value for each power of 2.
53. Using a timer to generate a random number, develop a program that displays random num-
bers between 1 and 47 (or whatever) for your state’s lottery.
54. Modify the program in Example 8–28 so it also displays the letters A, b, C, d, E, and F for a
hexadecimal seven-segment display.
55. Modify Example 8–42 to encrypt the message using an algorithm of your own design.
56. Develop a Decryption function (for a String) to accompany the encryption of question 55.
CHAPTER 9
8086/8088 Hardware Specifications
INTRODUCTION
In this chapter, the pin functions of both the 8086 and 8088 microprocessors are detailed and
information is provided on the following hardware topics: clock generation, bus buffering, bus
latching, timing, wait states, and minimum mode operation versus maximum mode operation.
These simple microprocessors are explained first, because of their less intricate structures, as an
introduction to the Intel microprocessor family.
Before it is possible to connect or interface anything to the microprocessor, it is necessary
to understand the pin functions and timing. These rudimentary microprocessors contain the
same basic pins as the latest Pentium 4 or Core2 microprocessor. Thus, the information in this
chapter is essential to a complete understanding of memory and I/O interfacing, which we
cover in the later chapters of the text.
CHAPTER OBJECTIVES
Upon completion of this chapter, you will be able to:
1. Describe the function of each 8086 and 8088 pin.
2. Understand the microprocessor’s DC characteristics and indicate its fan-out to common
logic families.
3. Use the clock generator chip (8284A) to provide the clock for the microprocessor.
4. Connect buffers and latches to the buses.
5. Interpret the timing diagrams.
6. Describe wait states and connect the circuitry required to cause various numbers of waits.
7. Explain the difference between minimum and maximum mode operation.
9–1 PIN-OUTS AND THE PIN FUNCTIONS
In this section, we explain the function and (in certain instances) the multiple functions of each
of the microprocessor’s pins. In addition, we discuss the DC characteristics to provide a basis for
understanding the later sections on buffering and latching.
302
8086/8088 HARDWARE SPECIFICATIONS 303
The Pin-Out
Figure 9–1 illustrates the pin-outs of the 8086 and 8088 microprocessors. As a close comparison
reveals, there is virtually no difference between these two microprocessors—both are packaged
in 40-pin dual in-line packages (DIPs).
As mentioned in Chapter 1, the 8086 is a 16-bit microprocessor with a 16-bit data bus and
the 8088 is a 16-bit microprocessor with an 8-bit data bus. (As the pin-outs show, the 8086 has
pin connections AD0–AD15, and the 8088 has pin connections AD0–AD7.) Data bus width there-
fore the only major difference between these microprocessors. This allows the 8086 to transfer
16-bit data more efficiently.
There is, however, a minor difference in one of the control signals. The 8086 has an M>IO
pin, and the 8088 has an IO/M pin. The only other hardware difference appears on Pin 34 of both
integrated circuits: on the 8088, it is an SS0 pin, whereas on the 8086, it is a BHE /S7 pin.
Power Supply Requirements
Both the 8086 and 8088 microprocessors require +5.0 V with a supply voltage tolerance of ±10
percent. The 8086 uses a maximum supply current of 360 mA, and the 8088 draws a maximum
of 340 mA. Both microprocessors operate in ambient temperatures of between 32° F and 180° F.
This range is not wide enough to be used outdoors in the winter or even in the summer, but
extended temperature-range versions of the 8086 and 8088 microprocessors are available. There
is also a CMOS version, which requires a very low supply current and has an extended temperature
range. The 80C88 and 80C86 are CMOS versions that require only 10 mA of power supply cur-
rent and function in temperature extremes of -40° F through +225° F.
DC Characteristics
It is impossible to connect anything to the pins of the microprocessor without knowing the input
current requirement for an input pin and the output current drive capability for an output pin.
This knowledge allows the hardware designer to select the proper interface components for use
with the microprocessor without the fear of damaging anything.
FIGURE 9–1 (a) The 16 AD0 AD8 8 16 AD0 AD8 8
pin-out of the 8086 in 15 AD1 AD9 7 15 AD1 AD9 7
maximum mode; (b) the 14 AD2 AD10 6 14 AD2 AD10 6
pin-out of the 8086 in 13 AD3 AD11 5 13 AD3 AD11 5
minimum mode. 12 AD4 AD12 4 12 AD4 AD12 4
11 AD5 AD13 3 11 AD5 AD13 3
10 AD6 AD14 2 10 AD6 AD14 2
AD7 AD15 39 AD7 AD15 39
9 9
A16/S3 38 A16/S3 38
19 CLK A17/S4 37 19 CLK A17/S4 37
A18/S5 36 A18/S5 36
18 INTR A19/S6 35 31 HOLD A19/S6 35
33 MX 18 INTR
17 NMI S0 26 33 MN ALE 25
22 READY S1 27 17 NMI BHE/S7 34
31 RQ/GT0 S2 28 22 READY 26
30 RQ/GT1 21 RST DEN 27
21 RST BHE/S7 34 23 TEST DT/R 30
23 TEST LOCK 29 HLDA 24
QS0 25 INTA 28
QS1 24 M/IO 32
RD 32 29
RD
WR
8086MAX 8086MIN
(a) (b)
304 CHAPTER 9
TABLE 9–1 Input Logic Level Voltage Current
characteristics of
the 8086 and 8088 0 0.8 V maximum ±10 μA maximum
microprocessors. 1 2.0 V minimum ±10 μA maximum
Input Characteristics. The input characteristics of these microprocessors are compatible with all the
standard logic components available today. Table 9–1 depicts the input voltage levels and the input
current requirements for any input pin on either microprocessor. The input current levels are very
small because the inputs are the gate connections of MOSFETs and represent only leakage currents.
Output Characteristics. Table 9–2 illustrates the output characteristics of all the output pins of
these microprocessors. The logic 1 voltage level of the 8086/8088 is compatible with that of most
standard logic families, but the logic 0 level is not. Standard logic circuits have a maximum logic 0
voltage of 0.4 V, and the 8086/8088 has a maximum of 0.45 V. Thus, there is a difference of 0.05 V.
This difference reduces the noise immunity from a standard level of 400 mV (0.8 V – 0.45 V)
to 350 mV. (The noise immunity is the difference between the logic 0 output voltage and the
logic 0 input voltage levels.) The reduction in noise immunity may result in problems with long
wire connections or too many loads. It is therefore recommended that no more than 10 loads of
any type or combination be connected to an output pin without buffering. If this loading factor is
exceeded, noise will begin to take its toll in timing problems.
Table 9–3 lists some of the more common logic families and the recommended fan-out
from the 8086/8088. The best choice of component types for the connection to an 8086/8088
output pin is an LS, 74ALS, or 74HC logic component. Note that some of the fan-out currents
calculate to more than 10 unit loads. It is therefore recommended that if a fan-out of more than
10 unit loads is required, the system should be buffered.
Pin Connections
AD7–AD0 The 8088 address/data bus lines are the multiplexed address data bus of the
8088 and contain the rightmost 8 bits of the memory address or I/O port number
whenever ALE is active (logic 1) or data whenever ALE is inactive (logic 0).
These pins are at their high-impedance state during a hold acknowledge.
TABLE 9–2 Output Logic Level Voltage Current
characteristics of
the 8086 and 8088 0 0.45V maximum 2.0 mA maximum
microprocessors. 1 2.4 V minimum –400 μA maximum
TABLE 9–3 Recommended Family Sink Current Source Current Fan-out
fan-out from any 8086/8088 pin
connection. TTL (74) -1.6 mA 40 μA 1
TTL (74LS) -0.4 mA 20 μA 5
TTL (74S) -2.0 mA 50 μA 1
TTL (74ALS) -0.1 mA 20 μA 10
TTL (74AS) -0.5 mA 25 μA 10
TTL (74F) -0.5 mA 25 μA 10
CMOS (74HC) - 10 μA 10 μA 10
CMOS (CD) - 10 μA 10 μA 10
NMOS - 10 μA 10 μA 10
8086/8088 HARDWARE SPECIFICATIONS 305
TABLE 9–4 Function of S4 S3 Function
status bits S3 and S4.
0 0 Extra segment
0 1 Stack segment
1 0 Code or no segment
1 1 Data segment
A15–A8 The 8088 address bus provides the upper-half memory address bits that are
AD15–AD8 present throughout a bus cycle. These address connections go to their high-
A19/S6–A16/S3 impedance state during a hold acknowledge.
RD The 8086 address/data bus lines compose the upper multiplexed
READY address/data bus on the 8086. These lines contain address bits A15–A8 when-
INTR ever ALE is a logic 1, and data bus connections D15–D8 when ALE is a logic 0.
TEST These pins enter a high-impedance state when a hold acknowledge occurs.
NMI
RESET The address/status bus bits are multiplexed to provide address signals
CLK A19–A16 and also status bits S6–S3. These pins also attain a high-impedance
VCC state during the hold acknowledge.
Status bit S6 is always a logic 0, bit S5 indicates the condition of the IF
flag bit, and S4 and S3 show which segment is accessed during the current
bus cycle. See Table 9–4 for the truth table of S4 and S3. These two status
bits could be used to address four separate 1M byte memory banks by
decoding them as A21 and A20.
Whenever the read signal is a logic 0, the data bus is receptive to data from
the memory or I/O devices connected to the system. This pin floats to its
high-impedance state during a hold acknowledge.
The READY input is controlled to insert wait states into the timing of the
microprocessor. If the READY pin is placed at a logic 0 level, the micro-
processor enters into wait states and remains idle. If the READY pin is placed
at a logic 1 level, it has no effect on the operation of the microprocessor.
Interrupt request is used to request a hardware interrupt. If INTR is held
high when IF = 1, the 8086/8088 enters an interrupt acknowledge cycle
(INTA becomes active) after the current instruction has completed execution.
The Test pin is an input that is tested by the WAIT instruction. If TEST is a
logic 0, the WAIT instruction functions as an NOP and if TEST is a logic 1,
the WAIT instruction waits for TEST to become a logic 0. The TEST pin
is most often connected to the 8087 numeric coprocessor.
The non-maskable interrupt input is similar to INTR except that the NMI
interrupt does not check to see whether the IF flag bit is a logic 1. If NMI is
activated, this interrupt input uses interrupt vector 2.
The reset input causes the microprocessor to reset itself if this pin is held
high for a minimum of four clocking periods. Whenever the 8086 or 8088 is
reset, it begins executing instructions at memory location FFFFOH and dis-
ables future interrupts by clearing the IF flag bit.
The clock pin provides the basic timing signal to the microprocessor. The clock
signal must have a duty cycle of 33 % (high for one third of the clocking period
and low for two thirds) to provide proper internal timing for the 8086/8088.
This power supply input provides a +5.0 V, ±10 % signal to the microprocessor.
306 CHAPTER 9
GND The ground connection is the return for the power supply. Note that the
MN/MX 8086/8088 microprocessors have two pins labeled GND—both must be
BHE S7 connected to ground for proper operation.
The minimum/maximum mode pin selects either minimum mode or max-
imum mode operation for the microprocessor. If minimum mode is
selected, the MN/MX pin must be connected directly to +5.0 V.
The bus high enable pin is used in the 8086 to enable the most-significant
data bus bits (D15–D8) during a read or a write operation. The state of S7 is
always a logic 1.
Minimum Mode Pins. Minimum mode operation of the 8086/8088 is obtained by connecting
the MN/MX pin directly to +5.0 V. Do not connect this pin to +5.0 V through a pull-up register,
or it will not function correctly.
IO/M or M/ IO The IO/M (8088) or the M/ IO (8086) pin selects memory or I/O. This pin
indicates that the microprocessor address bus contains either a memory
WR address or an I/O port address. This pin is at its high-impedance state dur-
ing a hold acknowledge.
INTA
ALE The write line is a strobe that indicates that the 8086/8088 is outputting
DT/ R data to a memory or I/O device. During the time that the WR is a logic 0,
DEN the data bus contains valid data for memory or I/O. This pin floats to a high-
HOLD impedance during a hold acknowledge.
HLDA The interrupt acknowledge signal is a response to the INTR input pin.
SS0 The INTA pin is normally used to gate the interrupt vector number onto the
data bus in response to an interrupt request.
Address latch enable shows that the 8086/8088 address/data bus contains
address information. This address can be a memory address or an I/O port
number. Note that the ALE signal does not float during a hold acknowledge.
The data transmit/receive signal shows that the microprocessor data bus is
transmitting (DT/R ϭ 1) or receiving (DT/R ϭ 0) data. This signal is
used to enable external data bus buffers.
Data bus enable activates external data bus buffers.
The hold input requests a direct memory access (DMA). If the HOLD sig-
nal is a logic 1, the microprocessor stops executing software and places its
address, data, and control bus at the high-impedance state. If the HOLD pin
is a logic 0, the microprocessor executes software normally.
Hold acknowledge indicates that the 8086/8088 has entered the hold state.
The SS0 status line is equivalent to the S0 pin in maximum mode operation
of the microprocessor. This signal is combined with IO/M and DT/R to
decode the function of the current bus cycle (see Table 9–5).
Maximum Mode Pins. In order to achieve maximum mode for use with external coprocessors,
connect the MN/MX pin to ground.
S2, S1, and S0 The status bits indicate the function of the current bus cycle. These signals are
normally decoded by the 8288 bus controller described later in this chapter.
RQ /GT1 and Table 9–6 shows the function of these three status bits in the maximum mode.
RQ>GT0
The request/grant pins request direct memory accesses (DMA) during
maximum mode operation. These lines are bidirectional and are used to
both request and grant a DMA operation.
8086/8088 HARDWARE SPECIFICATIONS 307
TABLE 9–5 Bus cycle status (8088)
using SS0. IO/M DT/R SS0 Function
0 0 0 Interrupt acknowledge
0 0 1 Memory read
0 1 0 Memory write
0 1 1 Halt
1 0 0 Opcode fetch
1 0 1 I/O read
1 1 0 I/O write
1 1 1 Passive
TABLE 9–6 Bus control function
generated by the bus controller S2 S1 S0 Function
(8288). 0 0 0 Interrupt acknowledge
0 0 1 I/O read
0 1 0 I/O write
0 1 1 Halt
1 0 0 Opcode fetch
1 0 1 Memory read
1 1 0 Memory write
1 1 1 Passive
TABLE 9–7 Queue status bits.
QS1 QS0 Function
0 0 Queue is idle
0 1 First byte of opcode
1 0 Queue is empty
1 1 Subsequent byte of opcode
LOCK The lock output is used to lock peripherals off the system. This pin is acti-
QS1 and QS0 vated by using the LOCK: prefix on any instruction.
The queue status bits show the status of the internal instruction queue.
These pins are provided for access by the numeric coprocessor (8087). See
Table 9–7 for the operation of the queue status bits.
9–2 CLOCK GENERATOR (8284A)
This section describes the clock generator (8284A) and the RESET signal, and introduces the
READY signal for the 8086/8088 microprocessors. (The READY signal and its associated cir-
cuitry are treated in detail in Section 9–5.)
The 8284A Clock Generator
The 8284A is an ancillary component to the 8086/8088 microprocessors. Without the clock gen-
erator, many additional circuits are required to generate the clock (CLK) in an 8086/8088-based
system. The 8284A provides the following basic functions or signals: clock generation, RESET
synchronization, READY synchronization, and a TTL-level peripheral clock signal. Figure 9–2
illustrates the pin-out of the 8284A clock generator.
308 CHAPTER 9
FIGURE 9–2 The pin-out of 3 AEN1 CLK 8
the 8284A clock generator. 7 AEN2 PCLK 2
14 EFI READY 5
RESET 10
12 OSC
17 X1
16 X2
15 ASYNC
1 CSYNC
F/C
13 RDY1
4 RDY2
6 RES
11
8284A
Pin Functions. The 8284A is an 18-pin integrated circuit designed specifically for use with the
8086/8088 microprocessor. The following is a list of each pin and its function.
AEN1 and AEN2 The address enable pins are provided to qualify the bus ready signals,
RDY1 and RDY2, respectively. Section 9–5 illustrates the use of these two
RDY1 and RDY2 pins, which are used to cause wait states, along with the RDY1 and RDY2
ASYNC inputs. Wait states are generated by the READY pin of the 8086/8088
READY microprocessors, which is controlled by these two inputs.
X1 and X2
F/C The bus ready inputs are provided, in conjunction with the AEN1 and
AEN2 pins, to cause wait states in an 8086/8088-based system.
CLK
The ready synchronization selection input selects either one or two stages
PCLK of synchronization for the RDY1 and RDY2 inputs.
OSC Ready is an output pin that connects to the 8086/8088 READY input. This
RES signal is synchronized with the RDY1 and RDY2 inputs.
RESET The crystal oscillator pins connect to an external crystal used as the timing
CSYNC source for the clock generator and all its functions.
GND
VCC The frequency/crystal select input chooses the clocking source for the
8284A. If this pin is held high, an external clock is provided to the EFI
input pin; if it is held low, the internal crystal oscillator provides the timing
signal. The external frequency input is used when the F/C pin is pulled
high. EFI supplies the timing whenever the F/C pin is high.
The clock output pin provides the CLK input signal to the 8086/8088
microprocessors and other components in the system. The CLK pin has an
output signal that is one third of the crystal or EFI input frequency, and has
a 33% duty cycle, which is required by the 8086/8088.
The peripheral clock signal is one sixth the crystal or EFI input frequency,
and has a 50% duty cycle. The PCLK output provides a clock signal to the
peripheral equipment in the system.
The oscillator output is a TTL-level signal that is at the same frequency as
the crystal or EFI input. The OSC output provides an EFI input to other
8284A clock generators in some multiple-processor systems.
The reset input is an active-low input to the 8284A. The RES pin is often
connected to an RC network that provides power-on resetting.
The reset output is connected to the 8086/8088 RESET input pin.
The clock synchronization pin is used whenever the EFI input provides
synchronization in systems with multiple processors. If the internal crystal
oscillator is used, this pin must be grounded.
The ground pin connects to ground.
This power supply pin connects to +5.0 V with a tolerance of ±10%.
8086/8088 HARDWARE SPECIFICATIONS 309
FIGURE 9–3 The internal
block diagram of the
8284A clock generator.
Operation of the 8284A
The 8284A is a relatively easy component to understand. Figure 9–3 illustrates the internal tim-
ing diagram of the 8284A clock generator.
Operation of the Clock Section. The top half of the logic diagram represents the clock and syn-
chronization section of the 8284A clock generator. As the diagram shows, the crystal oscillator
has two inputs: X1 and X2. If a crystal is attached to X1 and X2, the oscillator generates a square-
wave signal at the same frequency as the crystal. The square-wave signal is fed to an AND gate
and also to an inverting buffer that provides the OSC output signal. The OSC signal is sometimes
used as an EFI input to other 8284A circuits in a system.
An inspection of the AND gate reveals that when F/C is a logic 0, the oscillator output is steered
through to the divide-by-3 counter. If F/C is a logic 1, then EFI is steered through to the counter.
The output of the divide-by-3 counter generates the timing for ready synchronization, a
signal for another counter (divide-by-2), and the CLK signal to the 8086/8088 microprocessor.
The CLK signal is also buffered before it leaves the clock generator. Notice that the output of the
first counter feeds the second. These two cascaded counters provide the divide-by-6 output at
PCLK, the peripheral clock output.
Figure 9–4 shows how an 8284A is connected to the 8086/8088. Notice that F/C and
CSYNC are grounded to select the crystal oscillator, and that a 15 MHz crystal provides the nor-
mal 5 MHz clock signal to the 8086/8088, as well as a 2.5 MHz peripheral clock signal.
Operation of the Reset Section. The reset section of the 8284A is very simple: It consists of a
Schmitt trigger buffer and a single D-type flip-flop circuit. The D-type flip-flop ensures that the
timing requirements of the 8086/8088 RESET input are met. This circuit applies the RESET sig-
nal to the microprocessor on the negative edge (1-to-0 transition) of each clock. The 8086/8088
microprocessors sample RESET at the positive edge (0-to-1 transition) of the clocks; therefore,
this circuit meets the timing requirements of the 8086/8088.
Refer to Figure 9–4. Notice that an RC circuit provides a logic 0 to the RES input pin
when power is first applied to the system. After a short time, the RES input becomes a logic 1
because the capacitor charges toward +5.0 V through the resistor. A pushbutton switch allows the
microprocessor to be reset by the operator. Correct reset timing requires the RESET input to
come a logic 1 no later than four clocks after system power is applied, and to be held high for at
310 CHAPTER 9
FIGURE 9–4 The clock generator (8284A) and the 8086 and 8088 microprocessors illustrating
the connection for the clock and reset signals. A 15 MHz crystal provides the 5 MHz clock for the
microprocessor.
least 50 μs. The flip-flop makes certain that RESET goes high in four clocks, and the RC time
constant ensures that it stays high for at least 50 μs.
9–3 BUS BUFFERING AND LATCHING
Before the 8086/8088 microprocessors can be used with memory or I/O interfaces, their multi-
plexed buses must be demultiplexed. This section provides the detail required to demultiplex the
buses and illustrates how the buses are buffered for very large systems. (Because the maximum
fan-out is 10, the system must be buffered if it contains more than 10 other components.)
Demultiplexing the Buses
The address/data bus on the 8086/8088 is multiplexed (shared) to reduce the number of pins required
for the 8086/8088 microprocessor integrated circuit. Unfortunately, this burdens the hardware
designer with the task of extracting or demultiplexing information from these multiplexed pins.
Why not leave the buses multiplexed? Memory and I/O require that the address remains
valid and stable throughout a read or write cycle. If the buses are multiplexed, the address
changes at the memory and I/O, which causes them to read or write data in the wrong locations.
All computer systems have three buses: (1) an address bus that provides the memory and I/O
with the memory address or the I/O port number, (2) a data bus that transfers data between the micro-
processor and the memory and I/O in the system, and (3) a control bus that provides control signals
to the memory and I/O. These buses must be present in order to interface to memory and I/O.
Demultiplexing the 8088. Figure 9–5 illustrates the 8088 microprocessor and the components
required to demultiplex its buses. In this case, two 74LS373 or 74LS573 transparent latches are
used to demultiplex the address/data bus connections AD7–AD0 and the multiplexed address/
status connections A19/S6–A16/S3.
These transparent latches, which are like wires whenever the address latch enable pin
(ALE) becomes a logic 1, pass the inputs to the outputs. After a short time, ALE returns to its
logic 0 condition, which causes the latches to remember the inputs at the time of the change to a
8086/8088 HARDWARE SPECIFICATIONS 311
FIGURE 9–5 The 8088 microprocessor shown with a demultiplexed address bus. This is the
model used to build many 8088-based systems.
logic 0. In this case, A7–A0 are stored in the bottom latch and A19–A16 are stored in the top latch.
This yields a separate address bus with connections A19–A0. These address connections allow
the 8088 to address 1M byte of memory space. The fact that the data bus is separate allows it to
be connected to any 8-bit peripheral device or memory component.
Demultiplexing the 8086. Like the 8088, the 8086 system requires separate address, data, and
control buses. It differs primarily in the number of multiplexed pins. In the 8088, only AD7–AD0
and A19/S6–A16/S3 are multiplexed. In the 8086, the multiplexed pins include AD15–AD0
A19/S6–A16/S3, and BHE/S7. All of these signals must be demultiplexed.
Figure 9–6 illustrates a demultiplexed 8086 with all three buses: address (A19–A0 and
BHE), data (D15–D0), and control (M>IO, RD, and WR).
This circuit shown in Figure 9–6 is almost identical to the one pictured in Figure 9–5,
except that an additional 74LS373 latch has been added to demultiplex the address/data bus pins
AD15–AD8 and a BHE/S7 input has been added to the top 74LS373 to select the high-order
memory bank in the l6-bit memory system of the 8086. Here, the memory and I/O system see the
312 CHAPTER 9
FIGURE 9–6 The 8086 microprocessor shown with a demultiplexed address bus. This is the
model used to build many 8086-based systems.
8086 as a device with a 20-bit address bus (A19–A0), a l6-bit data bus (D15–D0), and a three-line
control bus (M>IO, RD, and WR).
The Buffered System
If more than 10 unit loads are attached to any bus pin, the entire 8086 or 8088 system must be
buffered. The demultiplexed pins are already buffered by the 74LS373 or 74LS573 latches,
which have been designed to drive the high-capacitance buses encountered in microcomputer
8086/8088 HARDWARE SPECIFICATIONS 313
systems. The buffer’s output currents have been increased so that more TTL unit loads may be
driven: A logic 0 output provides up to 32 mA of sink current, and a logic 1 output provides up
to 5.2 mA of source current.
A fully buffered signal will introduce a timing delay to the system. This causes no diffi-
culty unless memory or I/O devices are used, which function at near the maximum speed of the
bus. Section 9–4 discusses this problem and the time delays involved in more detail.
The Fully Buffered 8088. Figure 9–7 depicts a fully buffered 8088 microprocessor. Notice that
the remaining eight address pins, A15–A8, use a 74LS244 octal buffer; the eight data bus pins,
D7–D0, use a 74LS245 octal bidirectional bus buffer; and the control bus signals, M>IO, RD,
FIGURE 9–7 A fully buffered 8088 microprocessor.
314 CHAPTER 9
and WR, use a 74LS244 buffer. A fully buffered 8088 system requires two 74LS244s, one
74LS245, and two 74LS373s. The direction of the 74LS245 is controlled by the DT/R signal and
is enabled and disabled by the DEN signal.
The Fully Buffered 8086. Figure 9–8 illustrates a fully buffered 8086 microprocessor. Its
address pins are already buffered by the 74LS373 address latches; its data bus employs two
FIGURE 9–8 A fully buffered 8086 microprocessor.
8086/8088 HARDWARE SPECIFICATIONS 315
74LS245 octal bidirectional bus buffers; and the control bus signals, M>IO, RD, and WR use
a 74LS244 buffer. A fully buffered 8086 system requires one 74LS244, two 74LS245s, and three
74LS373s. The 8086 requires one more buffer than the 8088 because of the extra eight data bus
connections, D15–D8. It also has a BHE signal that is buffered for memory-bank selection.
9–4 BUS TIMING
It is essential to understand system bus timing before choosing a memory or I/O device for inter-
facing to the 8086 or 8088 microprocessors. This section provides insight into the operation of
the bus signals and the basic read and write timing of the 8086/8088. It is important to note that
we discuss only the times that affect memory and I/O interfacing in this section.
Basic Bus Operation
The three buses of the 8086 and 8088—address, data, and control—function exactly the same way
as those of any other microprocessor. If data are written to the memory (see the simplified timing
for write in Figure 9–9), the microprocessor outputs the memory address on the address bus, out-
puts the data to be written into memory on the data bus, and issues a write (WR) to memory and
IO>M = 0 for the 8088 and M>IO = 1 for the 8086. If data are read from the memory (see the sim-
plified timing for read in Figure 9–10), the microprocessor outputs the memory address on the
address bus, issues a read memory signal (RD), and accepts the data via the data bus.
Timing in General
The 8086/8088 microprocessors use the memory and I/O in periods called bus cycles. Each bus
cycle equals four system-clocking periods (T states). Newer microprocessors divide the bus
cycle into as few as two clocking periods. If the clock is operated at 5 MHz (the basic operating
frequency for these two microprocessors), one 8086/8088 bus cycle is complete in 800 ns. This
means that the microprocessor reads or writes data between itself and memory or I/O at a maxi-
mum rate of 1.25 million times a second. (Because of the internal queue, the 8086/8088 can exe-
cute 2.5 million instructions per second [MIPS] in bursts.) Other available versions of these
microprocessors operate at much higher transfer rates due to higher clock frequencies.
FIGURE 9–9 Simplified 8086/8088 write bus cycle.
316 CHAPTER 9
FIGURE 9–10 Simplified 8086/8088 read bus cycle.
During the first clocking period in a bus cycle, which is called T1, many things happen.
The address of the memory or I/O location is sent out via the address bus and the address/data
bus connections. (The address/data bus is multiplexed and sometimes contains memory-addressing
information, sometimes data.) During TI, control signals ALE, DT>R, and IO>M (8088) or
M>IO (8086) are also output. The IO>M or M>IO signal indicates whether the address bus con-
tains a memory address or an I/O device (port) number.
During T2, the 8086/8088 microprocessors issue the RD or WR signal, DEN, and in the
case of a write, the data to be written appear on the data bus. These events cause the memory or
I/O device to begin to perform a read or a write. The DEN signal turns on the data bus buffers, if
they are present in the system, so the memory or I/O can receive data to be written, or so the
microprocessor can accept the data read from the memory or I/O for a read operation. If this hap-
pens to be a write bus cycle, the data are sent out to the memory or I/O through the data bus.
READY is sampled at the end of T2, as illustrated in Figure 9–11. If READY is low at this
time, T3 becomes a wait state (Tw). (More detail is provided in Section 9–5.) This clocking period
is provided to allow the memory time to access data. If the bus cycle happens to be a read bus
cycle, the data bus is sampled at the end of T3.
In T4, all bus signals are deactivated in preparation for the next bus cycle. This is also the
time when the 8086/8088 samples the data bus connections for data that are read from memory
or I/O. In addition, at this point, the trailing edge of the WR signal transfers data to the memory
or I/O, which activates and writes when the WR signal returns to a logic 1 level.
Read Timing
Figure 9–11 also depicts the read timing for the 8088 microprocessor. The 8086 read timing is
identical except that the 8086 has 16 rather than eight data bus bits. A close look at this timing
diagram should allow you to identify all the main events described for each T state.
The most important item contained in the read timing diagram is the amount of time allowed
for the memory or I/O to read the data. Memory is chosen by its access time, which is the fixed
amount of time that the microprocessor allows it to access data for the read operation. It is therefore
extremely important that the memory chosen complies with the limitations of the system.
The microprocessor timing diagram does not provide a listing for memory access time.
Instead, it is necessary to combine several times to arrive at the access time. To find memory
8086/8088 HARDWARE SPECIFICATIONS 317
FIGURE 9–11 Minimum mode 8088 bus timing for a read operation.
access time in this diagram, first locate the point in T3 when data are sampled. If you examine the
timing diagram closely, you will notice a line that extends from the end of T3 down to the data
bus. At the end of T3, the microprocessor samples the data bus.
Memory access time starts when the address appears on the memory address bus and con-
tinues until the microprocessor samples the memory data at T3. Approximately three T states
elapse between these times. (See Figure 9–12 for the following times.) The address does not
appear until TCLAV time (110 ns if the clock is 5 MHz) after the start of T1. This means that
TCLAV time must be subtracted from the three clocking states (600 ns) that separate the appear-
ance of the address (T1) and the sampling of the data (T3). One other time must also be sub-
tracted: the data setup time (TDVCL), which occurs before T3. Memory access time is thus three
clocking states minus the sum of TCLAV and TDVCL. Because TDVCL is 30 ns with a 5 MHz clock,
the allowed memory access time is only 460 ns (access time = 600 ns - 110 ns - 30 ns).
The memory devices chosen for connection to the 8086/8088 operating at 5 MHz must be
able to access data in less than 460 ns, because of the time delay introduced by the address
decoders and buffers in the system. At least a 30- or 40-ns margin should exist for the operation
of these circuits. Therefore, the memory speed should be no slower than about 420 ns to operate
correctly with the 8086/8088 microprocessors.
FIGURE 9–12 8088 AC
characteristics.
318
8086/8088 HARDWARE SPECIFICATIONS 319
The only other timing factor that may affect memory operation is the width of the RD
strobe. On the timing diagram, the read strobe is given as TRLRH. The time for this strobe is 325 ns
(5 MHz clock rate), which is wide enough for almost all memory devices manufactured with an
access time of 400 ns or less.
Write Timing
Figure 9–13 illustrates the write-timing diagram for the 8088 microprocessor. (Again, the 8086
is nearly identical, so it need not be presented here in a separate timing diagram.)
The main differences between read and write timing are minimal. The RD strobe is replaced
by the WR strobe, the data bus contains information for the memory rather than information from
the memory, and DT>R remains a logic 1 instead of a logic 0 throughout the bus cycle.
When interfacing some memory devices, timing may be especially critical between the
point at which WR becomes a logic 1 and the time when the data are removed from the data bus.
This is the case because, as you will recall, memory data are written at the trailing edge of the
WR strobe. According to the timing diagram, this critical period is TWHDX or 88 ns when the
8088 is operated with a 5 MHz clock. Hold time is often much less than this; it is, in fact, often 0 ns
for memory devices. The width of the WR strobe is TWLWH or 340 ns at a 5 MHz clock rate. This
rate is compatible with most memory devices that have an access time of 400 ns or less.
FIGURE 9–13 Minimum mode 8088 write bus timing.
320 CHAPTER 9
9–5 READY AND THE WAIT STATE
As we mentioned earlier in this chapter, the READY input causes wait states for slower memory
and I/O components. A wait state (Tw) is an extra clocking period, inserted between T2 and T3 to
lengthen the bus cycle. If one wait state is inserted, then the memory access time, normally 460 ns
with a 5 MHz clock, is lengthened by one clocking period (200 ns) to 660 ns.
In this section, we discuss the READY synchronization circuitry inside the 8284A clock
generator, show how to insert one or more wait states selectively into the bus cycle, and examine
the READY input and the synchronization times it requires.
The READY Input
The READY input is sampled at the end of T2 and again, if applicable, in the middle of Tw. If
READY is a logic 0 at the end of T2, T3 is delayed and Tw is inserted between T2 and T3.
READY is next sampled at the middle of Tw to determine whether the next state is Tw or T3. It is
tested for a logic 0 on the 1-to-0 transition of the clock at the end of T2, and for a 1 on the 0-to-1
transition of the clock in the middle of Tw.
The READY input to the 8086/8088 has some stringent timing requirements. The timing
diagram in Figure 9–14 shows READY causing one wait state (Tw), along with the required
setup and hold times from the system clock. The timing requirement for this operation is met by
the internal READY synchronization circuitry of the 8284A clock generator. When the 8284A is
used for READY, the RDY (ready input to the 8284A) input occurs at the end of each T state.
RDY and the 8284A
RDY is the synchronized ready input to the 8284A clock generator. The timing diagram for this
input is provided in Figure 9–15. Although it differs from the timing for the READY input to the
FIGURE 9–14 8086/8088 READY input timing.
FIGURE 9–15 8284A RDY input timing.
8086/8088 HARDWARE SPECIFICATIONS 321
FIGURE 9–16 The internal
block diagram of the 8284A
clock generator. (Courtesy of
Intel Corporation.)
8086/8088, the internal 8284A circuitry guarantees the accuracy of the READY synchronization
provided to the 8086/8088 microprocessors.
Figure 9–16 again depicts the internal structure of the 8284A. The bottom half of this dia-
gram is the READY synchronization circuitry. At the leftmost side, the RDY1 and AEN1 inputs
are ANDed, as are the RDY2 and AEN2 inputs. The outputs of the AND gates are then ORed to
generate the input to the one or two stages of synchronization. In order to obtain a logic 1 at the
inputs to the flip-flops, RDY1 ANDed with AEN1 must be active or RDY2 ANDed with AEN2
must be active.
The ASYNC input selects one stage of synchronization when it is a logic 1 and two stages
when it is a logic 0. If one stage is selected, then the RDY signal is kept from reaching the
8086/8088 READY pin until the next negative edge of the clock. If two stages are selected, the
first positive edge of the clock captures RDY in the first flip-flop. The output of this flip-flop is
fed to the second flip-flop, so on the next negative edge of the clock, the second flip-flop
captures RDY.
Figure 9–17 illustrates a circuit used to introduce almost any number of wait states for the
8086/8088 microprocessors. Here, an 8-bit serial shift register (74LS164) shifts a logic 0 for one
or more clock periods from one of its Q outputs through to the RDY1 input of the 8284A. With
appropriate strapping, this circuit can provide various numbers of wait states. Notice also how
the shift register is cleared back to its starting point. The output of the register is forced high
when the RD, WR, and INTA pins are all logic 1s. These three signals are high until state T2, so
the shift register shifts for the first time when the positive edge of the T2 arrives. If one wait is
desired, output QB is connected to the OR gate. If two waits are desired, output QC is connected,
and so forth.
Notice in Figure 9–17 that this circuit does not always generate wait states. It is enabled
from the memory only for memory devices that require the insertion of waits. If the selection
signal from a memory device is a logic 0, the device is selected; then this circuit will generate a
wait state.
Figure 9–18 illustrates the timing diagram for this shift register wait state generator when
it is wired to insert one wait state. The timing diagram also illustrates the internal contents of the
shift register’s flip-flops to present a more detailed view of its operation. In this example, one
wait state is generated.
FIGURE 9–17 A circuit that will cause between 0 and 7 wait states.
FIGURE 9–18 Wait state generation timing of the circuit of Figure 9–17.
322
8086/8088 HARDWARE SPECIFICATIONS 323
9–6 MINIMUM MODE VERSUS MAXIMUM MODE
There are two available modes of operation for the 8086/8088 microprocessors: minimum mode
and maximum mode. Minimum mode operation is obtained by connecting the mode selection
pin MN>MX to +5.0 V, and maximum mode is selected by grounding this pin. Both modes
enable different control structures for the 8086/8088 microprocessors. The mode of operation
provided by minimum mode is similar to that of the 8085A, the most recent Intel 8-bit micro-
processor. The maximum mode is unique and designed to be used whenever a coprocessor exists
in a system. Note that the maximum mode was dropped from the Intel family beginning with the
80286 microprocessor.
Minimum Mode Operation
Minimum mode operation is the least expensive way to operate the 8086/8088 microprocessors
(see Figure 9–19 for the minimum mode 8088 system). It costs less because all the control sig-
nals for the memory and I/O are generated by the microprocessor. These control signals are iden-
tical to those of the Intel 8085A, an earlier 8-bit microprocessor. The minimum mode allows the
8085A 8-bit peripherals to be used with the 8086/8088 without any special considerations.
Maximum Mode Operation
Maximum mode operation differs from minimum mode in that some of the control signals must
be externally generated. This requires the addition of an external bus controller—the 8288 bus
controller (see Figure 9–20 for the maximum mode 8088 system). There are not enough pins on
the 8086/8088 for bus control during maximum mode because new pins and new features have
replaced some of them. Maximum mode is used only when the system contains external
coprocessors such as the 8087 arithmetic coprocessor.
FIGURE 9–19 Minimum mode 8088 system.
324 CHAPTER 9
FIGURE 9–20 Maximum mode 8088 system.
The 8288 Bus Controller
An 8086/8088 system that is operated in maximum mode must have an 8288 bus controller
to provide the signals eliminated from the 8086/8088 by the maximum mode operation.
Figure 9–21 illustrates the block diagram and pin-out of the 8288 bus controller.
Notice that the control bus developed by the 8288 bus controller contains separate signals
for I/O (IORC and IOWC) and memory (MRDC and MWTC). It also contains advanced mem-
ory (AMWC) and I/O (AIOWC) write strobes, and the INTA signal. These signals replace the
minimum mode ALE, WR, IO/M, DT/R, DEN, and INTA, which are lost when the 8086/8088
microprocessors are operated in the maximum mode.
FIGURE 9–21 The 8288 bus controller; (a) block diagram and (b) pin-out.
8086/8088 HARDWARE SPECIFICATIONS 325
Pin Functions
The following list provides a description of each pin of the 8288 bus controller.
S2, S1, and S0 Status inputs are connected to the status output pins on the 8086/8088
microprocessor. These three signals are decoded to generate the timing sig-
CLK nals for the system.
ALE
DEN The clock input provides internal timing and must be connected to the CLK
output pin of the 8284A clock generator.
DT>R
AEN The address latch enable output is used to demultiplex the address/data
CEN bus.
IOB
AIOWC The data bus enable pin controls the bidirectional data bus buffers in the
IORC system. Note that this is an active high output pin that is the opposite polar-
IOWC ity from the DEN signal found on the microprocessor when operated in the
AMWT minimum mode.
MWTC
MRDC The data transmit/receive signal is output by the 8288 to control the direc-
INTA tion of the bidirectional data bus buffers.
MCE>PDEN
The address enable input causes the 8288 to enable the memory control
signals.
The control enable input enables the command output pins on the 8288.
The I/O bus mode input selects either the I/O bus mode or system bus
mode operation.
The advanced I/O write is a command output used to provide I/O with an
advanced I/O write control signal.
The I/O read command output provides I/O with its read control signal.
The I/O write command output provides I/O with its main write signal.
The advanced memory write control pin provides memory with an early
or advanced write signal.
The memory write control pin provides memory with its normal write con-
trol signal.
The memory read control pin provides memory with a read control signal.
The interrupt acknowledge output acknowledges an interrupt request
input applied to the INTR pin.
The master cascade/peripheral data output selects cascade operation for
an interrupt controller if IOB is grounded, and enables the I/O bus trans-
ceivers if IOB is tied high.
9–7 SUMMARY
1. The main differences between the 8086 and 8088 are (1) an 8-bit data bus on the 8088 and a
16-bit data bus on the 8086, (2) an SS0 pin on the 8088 in place of BHE /S7 on the 8086, and
(3) an IO/M pin on the 8088 instead of an M/ IO on the 8086.
2. Both the 8086 and 8088 require a single +5.0 V power supply with a tolerance of ±10%.
3. The 8086/8088 microprocessors are TTL-compatible if the noise immunity figure is derated
to 350 mV from the customary 400 mV.
4. The 8086/8088 microprocessors can drive one 74XX, five 74LSXX, one 74SXX, ten
74ALSXX, and ten 74HCXX unit loads.
326 CHAPTER 9
5. The 8284A clock generator provides the system clock (CLK), READY synchronization, and
RESET synchronization.
6. The standard 5 MHz 8086/8088 operating frequency is obtained by attaching a 15 MHz
crystal to the 8284A clock generator. The PCLK output contains a TTL-compatible signal at
one half the CLK frequency.
7. Whenever the 8086/8088 microprocessors are reset, they begin executing software at mem-
ory location FFFF0H (FFFF:0000) with the interrupt request pin disabled.
8. Because the 8086/8088 buses are multiplexed and most memory and I/O devices aren’t, the
system must be demultiplexed before interfacing with memory or I/O. Demultiplexing is
accomplished by an 8-bit latch whose clock pulse is obtained from the ALE signal.
9. In a large system, the buses must be buffered because the 8086/8088 microprocessors are
capable of driving only 10 unit loads, and large systems often have many more.
10. Bus timing is very important to the remaining chapters in the text. A bus cycle that consists
of four clocking periods acts as the basic system timing. Each bus cycle is able to read or
write data between the microprocessor and the memory or I/O system.
11. A bus cycle is broken into four states, or T periods: T1 is used by the microprocessor to send
the address to the memory or I/O and the ALE signal to the demultiplexers; T2 is used to
send data to memory for a write and to test the READY pin and activate control signals RD
or WR; T3 allows the memory time to access data and allows data to be transferred between
the microprocessor and the memory or I/O; and T4 is where data are written.
12. The 8086/8088 microprocessors allow the memory and I/O 460 ns to access data when they
are operated with a 5 MHz clock.
13. Wait states (Tw) stretch the bus cycle by one or more clocking periods to allow the memory
and I/O additional access time. Wait states are inserted by controlling the READY input to
the 8086/8088. READY is sampled at the end of T2 and during Tw.
14. Minimum mode operation is similar to that of the Intel 8085A microprocessor, whereas
maximum mode operation is new and specifically designed for the operation of the 8087
arithmetic coprocessor.
15. The 8288 bus controller must be used in the maximum mode to provide the control bus sig-
nals to the memory and I/O. This is because the maximum mode operation of the 8086/8088
removes some of the system’s control signal lines in favor of control signals for the
coprocessors. The 8288 reconstructs these removed control signals.
9–8 QUESTIONS AND PROBLEMS
1. List the differences between the 8086 and the 8088 microprocessors.
2. Is the 8086/8088 TTL-compatible? Explain your answer.
3. What is the fan-out from the 8086/8088 to the following devices:
(a) 74XXX TTL
(b) 74ALSXXX TTL
(c) 74HCXXX CMOS
4. What information appears on the address/data bus of the 8088 while ALE is active?
5. What are the purposes of status bits S3 and S4?
6. What condition does a logic 0 on the 8086/8088 RD pin indicate?
7. Explain the operation of the TEST pin and the WAIT instruction.
8. Describe the signal that is applied to the CLK input pin of the 8086/8088 microprocessors.
9. What mode of operation is selected when MN>MX is grounded?
10. What does the WR strobe signal from the 8086/8088 indicate about the operation of the
8086/8088?
8086/8088 HARDWARE SPECIFICATIONS 327
11. When does ALE float to its high-impedance state?
12. When DT>R is a logic 1, what condition does it indicate about the operation of the
8086/8088?
13. What happens when the HOLD input to the 8086/8088 is placed at its logic 1 level?
14. What three minimum mode 8086/8088 pins are decoded to discover whether the processor is
halted?
15. Explain the operation of the LOCK pin.
16. What conditions do the QS1 and QS0 pins indicate about the 8086/8088?
17. What three housekeeping chores are provided by the 8284A clock generator?
18. By what factor does the 8284A clock generator divide the crystal oscillator’s output
frequency?
19. If the F>C pin is placed at a logic 1 level, the crystal oscillator is disabled. Where is the
timing input signal attached to the 8284A under this condition?
20. The PCLK output of the 8284A is ____________ MHz if the crystal oscillator is operating
at 14 MHz.
21. The RES input to the 8284A is placed at a logic ____________ level in order to reset the
8086/8088.
22. Which bus connections on the 8086 microprocessor are typically demultiplexed?
23. Which bus connections on the 8088 microprocessor are typically demultiplexed?
24. Which TTL-integrated circuit is often used to demultiplex the buses on the 8086/8088?
25. What is the purpose of the demultiplexed BHE signal on the 8086 microprocessor?
26. Why are buffers often required in an 8086/8088-based system?
27. What 8086/8088 signal is used to select the direction of the data flows through the 74LS245
bidirectional bus buffer?
28. A bus cycle is equal to clocking ____________ periods.
29. If the CLK input to the 8086/8088 is 4 MHz, how long is one bus cycle?
30. What two 8086/8088 operations occur during a bus cycle?
31. How many MIPS is the 8086/8088 capable of obtaining when operated with a 10 MHz
clock?
32. Briefly describe the purpose of each T state listed:
(a) T1
(b) T2
(c) T3
(d) T4
(e) Tw
33. How much time is allowed for memory access when the 8086/8088 is operated with a
5 MHz clock?
34. How wide is DEN if the 8088 is operated with a 5 MHz clock?
35. If the READY pin is grounded, it will introduce____________ states into the bus cycle of
the 8086/8088.
36. What does the ASYNC input to the 8284A accomplish?
37. What logic levels must be applied to AEN1 and RDY1 to obtain a logic 1 at the READY pin?
(Assume that AEN2 is at a logic 1 level.)
38. Contrast minimum and maximum mode 8086/8088 operation.
39. What main function is provided by the 8288 bus controller when used with 8086/8088 max-
imum mode operation?
CHAPTER 10
Memory Interface
10–1 INTRODUCTION
328 Whether simple or complex, every microprocessor-based system has a memory system. The
Intel family of microprocessors is no different from any other in this respect. Almost all sys-
tems contain two main types of memory: read-only memory (ROM) and random access
memory (RAM) or read/write memory. Read-only memory contains system software and
permanent system data, while RAM contains temporary data and application software. This
chapter explains how to interface both memory types to the Intel family of microprocessors.
We demonstrate memory interface to an 8-, 16-, 32-, and 64-bit data bus by using various
memory address sizes. This allows virtually any microprocessor to be interfaced to any
memory system.
CHAPTER OBJECTIVES
Upon completion of this chapter, you will be able to:
1. Decode the memory address and use the outputs of the decoder to select various memory
components.
2. Use programmable logic devices (PLDs) to decode memory addresses.
3. Explain how to interface both RAM and ROM to a microprocessor.
4. Explain how error correction code (ECC) is used with memory.
5. Interface memory to an 8-, 16-, 32-, and 64-bit data bus.
6. Explain the operation of a dynamic RAM controller.
7. Interface dynamic RAM to the microprocessor.
MEMORY DEVICES
Before attempting to interface memory to the microprocessor, it is essential to completely under-
stand the operation of memory components. In this section, we explain the functions of the four
common types of memory: read-only memory (ROM), flash memory (EEPROM), static random
access memory (SRAM), and dynamic random access memory (DRAM).
MEMORY INTERFACE 329
Memory Pin Connections
Pin connections common to all memory devices are the address inputs, data outputs or input/
outputs, some type of selection input, and at least one control input used to select a read or write
operation. See Figure 10–1 for ROM and RAM generic-memory devices.
Address Connections. All memory devices have address inputs that select a memory location
within the memory device. Address inputs are almost always labeled from A0, the least signifi-
cant address input, to An where subscript n can be any value but is always labeled as one less
than the total number of address pins. For example, a memory device with 10 address pins has its
address pins labeled from A0 to A9. The number of address pins found on a memory device is
determined by the number of memory locations found within it.
Today, the more common memory devices have between 1K (1024) to 1G (1,073,741,824)
memory locations, with 4G and larger memory location devices on the horizon. A 1K memory
device has 10 address pins (A0–A9); therefore, 10 address inputs are required to select any of its
1024 memory locations. It takes a 10-bit binary number (1024 different combinations) to select
any single location on a 1024-location device. If a memory device has 11 address connections
(A0–A11), it has 2048 (2K) internal memory locations. The number of memory locations can
thus be extrapolated from the number of address pins. For example, a 4K memory device has
12 address connections, an 8K device has 13, and so forth. A device that contains 1M locations
requires a 20-bit address (A0–A19).
The number 400H represents a 1K-byte section of the memory system. If a memory device is
decoded to begin at memory address 10000H and it is a 1K device, its last location is at address
103FFH—one location less than 400H. Another important hexadecimal number to remember is
1000H, because 1000H is 4K. A memory device that contains a starting address of 14000H that is 4K
bytes long ends at location 14FFFH—one location less than 1000H. A third number is 64K, or
10000H. A memory that starts at location 30000H and ends at location 3FFFFH is a 64K-byte mem-
ory. Finally, because 1M of memory is common, a 1M memory contains 100000H memory locations.
Data Connections. All memory devices have a set of data outputs or input/outputs. The device
illustrated in Figure 10–1 has a common set of input/output (I/O) connections. Today, many
memory devices have bidirectional common I/O pins.
The data connections are the points at which data are entered for storage or extracted for
reading. Data pins on memory devices are labeled D0 through D7 for an 8-bit-wide memory
FIGURE 10–1 A pseudo-
memory component illustrat-
ing the address, data, and
control connections.