7526 DCT Reference


CFR File Download Sequence

CFR file download sequence Downloading of CFRs is more complex than downloading of other files because the CFR is an executable file image which must be adjusted for the location of the CFR in the terminal's memory.

Overview

Space is allocated in the terminal by the terminal controller via CMD MB. The terminal's response is the segment address (on a paragraph boundary) at which the CFR will be stored in the terminal. The terminal controller must then adjust all the absolute references in the CFR executable file (EXE file) and transmit the adjusted image one block at a time using CMD MC. When the entire image has been sent, the terminal controller sends CMD MD to notify the terminal to initialize the CFR.

The size of the CFR is derived from values found in the EXE header (refer to Table 13. ). Offsets 04 and 05 of the EXE file image give the size of the file in 512 byte increments, offsets 02 and 03 are the file size modulo 512, offsets 08 and 09 give the size of the EXE header, and offsets 0A and 0B give the minimum number of paragraphs required above the loaded image. For a more detailed explanation of the EXE file format created by LINK, see the DOS Technical Reference manual chapter titled "EXE File Structure and Loading".

Once the resident size of the CFR header and code has been determined, use CMD MB to allocate space. The 7526 responds to this command with an address representing the segment at which the program loads. This address points to the CFR header, and the actual start of the CFR code (its base address) will be 32 bytes higher in memory. Use this base address to relocate the CFR image.

The final step in the relocation process is building a JMP (jump) instruction in the 32-byte CFR load header which directs the execution to the entry point of the relocated image. The JMP instruction uses the first 5 bytes of the CFR header, and the remaining 27 bytes of the header are unused and should be set to zero.

Once the relocation of the CFR takes place, downloading may be performed using CMD MC. After the relocated CFR image has been downloaded, the CFR is activated by CMD MD.

Disk EXE image:


+-----+                   +                         +
| EXE |
| hea |
| der |
| ,   |
| var |
| iab |
| le  |
| len |
| gth |
+-----+                   +                         +
| EXE |
| pro |
| gra |
| m   |
| ima |
| ge  |
+-----+

Terminal image:


+-----+                   +                         +
| 32- |
| byt |
| e   |
| hea |
| der |
| Fir |
| st  |
| 5   |
| byt |
| es  |
| are |
| a   |
| jum |
| p   |
| ins |
| tru |
| cti |
| on  |
| to  |
| the |
| ent |
| ry  |
| poi |
| nt  |
| of  |
| the |
| rel |
| oca |
| ted |
| ima |
| ge  |
| bel |
| ow. |
| Rem |
| ain |
| ing |
| 27  |
| byt |
| es  |
| res |
| erv |
| ed. |
+-----+                   +                         +
| Rel |
| oca |
| ted |
| EXE |
| pro |
| gra |
| m   |
| ima |
| ge  |
| (si |
| ze  |
| is  |
| tot |
| al  |
| EXE |
| fil |
| e   |
| siz |
| e   |
| min |
| us  |
| the |
| EXE |
| hea |
| der |
| siz |
| e)  |
+-----+

Table 13. DOS EXE File Header Format


+================+========+=========================+========================+
|   Hex Offset   | Contents                                                  |
+================+========+=========================+========================+
|     00-01      | 4DH, 5AH &mdash. This is the link program's signature to  |
|                | mark the file as a valid .EXE file.                       |
+----------------+--------+-------------------------+------------------------+
|     02-03      | Length of image modulo 512 (remainder after dividing the  |
|                | load module image size by 512.)                           |
+----------------+--------+-------------------------+------------------------+
|     04-05      | Size of the file, in 512-byte increments (pages),         |
|                | including the header.                                     |
+----------------+--------+-------------------------+------------------------+
|     06-07      | Number of relocation table items that follow the          |
|                | formatted portion of the header.                          |
+----------------+--------+-------------------------+------------------------+
|     08-09      | Size of the header in 16-byte increments (paragraphs).    |
|                | This is used to locate the beginning of the load module   |
|                | in the file.                                              |
+----------------+--------+-------------------------+------------------------+
|     0A-0B      | Minimum number of 16-byte paragraphs required above the   |
|                | end of the loaded program.                                |
+----------------+--------+-------------------------+------------------------+
|     0C-0D      | Maximum number of 16-byte paragraphs required above the   |
|                | end of the loaded program.                                |
+----------------+--------+-------------------------+------------------------+
|     0E-0F      | Offset of stack segment in load module (in segment form). |
+----------------+--------+-------------------------+------------------------+
|     10-11      | Value to be in the SP register when the module is given   |
|                | control.                                                  |
+----------------+--------+-------------------------+------------------------+
|     12-13      | Word checksum&mdash.negative sum of all the words in the  |
|                | file, ignoring overflow.                                  |
+----------------+--------+-------------------------+------------------------+
|     14-15      | Value to be in the IP register when the module is given   |
|                | control.                                                  |
+----------------+--------+-------------------------+------------------------+
|     16-17      | Offset of code segment within load module (in segment     |
|                | form).                                                    |
+----------------+--------+-------------------------+------------------------+
|     18-19      | Offset of first relocation item within the file.          |
+----------------+--------+-------------------------+------------------------+
|     1A-1B      | Overlay number (0 for resident part of the program).      |
+----------------+-----------------------------------------------------------+

Procedure

An outline of the CFR allocation, fix-up, and load process is presented below. The DOWNLOAD.TXT file on the 7526 Custom Function Routine Programming Support diskette provides further descriptions of the process and fragments of C code useful in creating a downloader.

  1. Open .EXE file of the CFR.

  2. Read .EXE file header (through at least byte 19h) to obtain various pointers and values required for the allocation and fix-up. (Refer to Table 13. .)

  3. Start the CFR load image with a blank, 32-byte CFR file header. This image may be on disk or in memory.

  4. Skip to the code section of the .EXE file, using the offset in bytes 8-9 of the header.

  5. Copy code section (to the end of .EXE file) into the CFR load image.

  6. The header portion of the EXE file is used to determine how much memory must be allocated in the 7526 for the CFR.

    Determine .EXE file size, "x", from:

    1. .EXE header offset 2-3 (Image size modulo 512: Add to size).

    2. .EXE header offset 4-5 (image size in 512 byte pages, rounded up to the next integer - if offset 2-3 is zero then multiply by 512 and add, otherwise decrement offset 4-5 by one and then multiply by 512 and add to the size).

    3. .EXE header offset 8-9 (Header size: Subtract from the size).

    4. .EXE header offset A-B (Minimum number of 16-byte paragraphs required above the loaded program: Multiply by 16 and add to size).

    5. CFR header size (32 bytes: Add to the size).
    The resulting equation is:
     x =  (  ( (offset 04-05h) X 512) +
               (offset 02-03h)        +
             ( (offset 0A-0Bh) X 16)  -
               (offset 08-09h)        +
             32
          )
     if ((offset 02-03h)>0)
        x = x - 512;
    

  7. Convert the size "x" from above to paragraphs (16-byte increments are required. Round the number of paragraphs up to the next highest number if (x modulo 16) is not zero (using an integer math construction such as "(x + 15)/16").

  8. Allocate the space required for the file image in the terminal using CMD MB and the paragraph size from above. The command returns the starting segment of the CFR load image. This segment locates the CFR header; the actual code portion of the CFR starts 32 bytes (2 paragraphs) in higher memory. Thus, the fix-up segment value used in the fix-up process is this starting segment plus 2.

    Note:

    The two bytes used in the size requested and the address returned have the high-order byte first, followed by the low-order byte. This will most likely be in the opposite order from the method of storage in personal computer systems (that is Intel reverse order storage).

  9. Using this fix-up segment value, complete the CFR header jump instruction and the fix-up of relocatable items in the code:

    1. Place a jump instruction at the beginning of the CFR header in the CFR load image. This causes a jump to the relocated entry point of the code image. The jump instruction consists of 5 bytes, defined as follows:
      Byte
      1
      8088 jump instruction&mdash.always EAh
      2-3
      Memory offset for jump destination. Set to equal offsets 14-15h from the EXE header.
      4-5
      Memory segment for jump destination. Set this to equal offsets 16-17h from the EXE header plus the fix-up segment value found in step 8 (return from MB command plus 2 paragraphs).

    2. Perform the relocation of all absolute references in the EXE image. The base address for relocation is the address returned by the CMD MB plus 2 paragraphs.

      Based on .EXE header offsets 18-19 (first relocation item) and 6-7 (number of relocation items), for each relocation item (2 byte segment and 2 byte offset):

      1. Convert to the location in the .EXE code (segment*16+offset). Take into consideration the 32-byte CFR header at the beginning of the image. The relocation item is actually 32 bytes further into the CFR load image than is indicated by the segment and offset of the relocation item.

      2. Seek to that position in the relocation file/RAM image.

      3. Read the relocatable segment value from the relocation image.

      4. Add the base address for relocation to that segment value.

      5. Re-seek back to the relocation segment and write the re-located segment value there.

  10. Download the image created in step 5 using CMD MC. Start at the beginning of the CFR load image and work all the way to the end of the image.

  11. Execute CMD MD to complete the download sequence.
          TERMINAL
         CONTROLLER             DCT
         __________             ___
 
         CMD MB    ----&rarrow.             Allocate space
                        &larrow.----   RSP  Allocation address
 
         Create download image, do relocations
 
         CMD MC    ----&rarrow.             Send image blocks
                        &larrow.----   ACK
           .
           .
         CMD 1          ----&rarrow.        Status Request
                        &larrow.----   RSP
         ACK       ----&rarrow.
         CMD MD    ----&rarrow.             CFR load complete
                        &larrow.----   ACK