home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / hf / dsp / source / loader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-10  |  4.5 KB  |  171 lines

  1. /*  LOADER.C -- Download DSP56001 programs to the DSP CARD 3
  2.  *
  3.  *  Copyright (C) by Alef Null 1990, 1991
  4.  *  Author(s): Jarkko Vuori, OH2LNS
  5.  *  Modification(s):
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include <memory.h>
  13. #include "dspio.h"
  14.  
  15.  
  16. /* extended downloader code */
  17. static long extloader[] = {
  18.     0x0003F8, 0x300A1B, 0x61F400, 0x0011E0, 0x061F80, 0x000007, 0x07D884, 0x075984, 0x0AF080, 0x0011E0,
  19.     0x0AA983, 0x0011EA, 0x0AA980, 0x0011E2, 0x084E2B, 0x300003, 0x0AF0AA, 0x0011FC, 0x0AF080, 0x0011E0, 0x0AA980,
  20.     0x0011E0, 0x2D0100, 0x2D0205, 0x0AF0AA, 0x0011F9, 0x200005, 0x0AF0AA, 0x0011F6, 0x0858EB, 0x0AF080, 0x0011E0,
  21.     0x0858AB, 0x0AF080, 0x0011E0, 0x08586B, 0x0AF080, 0x0011E0, 0x0502BA, 0x0000B9, 0x0C0000
  22. };
  23.  
  24. static struct {
  25.     char      *name;
  26.     unsigned       words,
  27.            maxaddress;
  28.     long      *data;
  29.     unsigned char *touched;
  30. } memspaces[3] = {
  31.     { "P", 0, 0x11df, },    // extended loader in 11E0H - 11FFH
  32.     { "X", 0, 0x08ff, },
  33.     { "Y", 0, 0x08ff, }
  34. };
  35.  
  36. /*
  37.  * Toggle HF0 flag
  38.  */
  39. static void ToggleHF0(void) {
  40.     WriteByte(ICR, 0x08); WriteByte(ICR, 0x00);
  41. }
  42.  
  43.  
  44. /*
  45.  * Download given datablock
  46.  */
  47. static void DownloadBlock(long *data, unsigned len) {
  48.     register long *pp;
  49.  
  50.     //WriteByte(ICR, 0x02);
  51.     for (pp = data; pp < data+len; pp++) {
  52.     WriteByte(TXH, (unsigned)(*pp >> 16));
  53.     WriteByte(TXM, (unsigned)(*pp >> 8));
  54.     WriteByte(TXL, (unsigned)*pp);
  55.     }
  56. }
  57.  
  58.  
  59. /*
  60.  * Download previously opened file
  61.  */
  62. void DownLoad(int fMsg) {
  63.     DATASPACE    space;
  64.     long    word;
  65.     unsigned    address;
  66.     int     i, fResult, fInternal = -1;
  67.  
  68.     ResetCard();
  69.     for (i = p; i <= y; i++)
  70.     if (!(
  71.         (memspaces[i].data      = calloc(memspaces[i].maxaddress+1, sizeof(long)))          &&
  72.         (memspaces[i].touched = calloc(memspaces[i].maxaddress+1, sizeof(unsigned char)))
  73.     )) {
  74.         fprintf(stderr, "loader error: not enought memory\n");
  75.         return;
  76.     }
  77.  
  78.     /* read program from file to memspaces */
  79.     while (!(fResult = ReadWord(&space, &address, &word))) {
  80.     if (space != p || address >= 512)
  81.         fInternal = 0;
  82.  
  83.     if (address > memspaces[space].maxaddress) {
  84.         fprintf(stderr, "loader error: too big %s space, allowed range %04XH-%04XH\n", memspaces[space].name, 0, memspaces[space].maxaddress);
  85.         return;
  86.     }
  87.  
  88.     memspaces[space].data[address]      = word;
  89.     memspaces[space].words          = max(memspaces[space].words, address+1);
  90.     memspaces[space].touched[address] = -1;
  91.     }
  92.     if (fResult < 0)
  93.     return;
  94.  
  95.     /* then send it to the DSP CARD 3 */
  96.     if (fInternal) {
  97.     /* only internal P memory used, use internal 56001 bootloader only */
  98.     DownloadBlock(memspaces[p].data, memspaces[p].words);
  99.     ToggleHF0();
  100.     if (fMsg)
  101.         printf("%u words to P space downloaded\n", memspaces[p].words);
  102.     } else {
  103.     /* external memories (or internal datamemories used), first download the extented loader */
  104.     DownloadBlock(extloader, sizeof(extloader)/sizeof(long));
  105.  
  106.     for (i = p; i <= y; i++)
  107.         if (memspaces[i].words) {
  108.         /* select space */
  109.         ToggleHF0();
  110.         WriteByte(TXH, 0); WriteByte(TXM, 0); WriteByte(TXL, i+1);
  111.  
  112.         /* then download it */
  113.         DownloadBlock(memspaces[i].data, memspaces[i].words);
  114.         }
  115.  
  116.     /* tell to the extended loader to start program */
  117.     ToggleHF0();
  118.     WriteByte(TXH, 0); WriteByte(TXM, 0); WriteByte(TXL, 0);
  119.     if (fMsg)
  120.         printf("%u,%u,%u words to P,X,Y spaces downloaded\n", memspaces[p].words,
  121.                                   memspaces[x].words,
  122.                                   memspaces[y].words);
  123.     }
  124. }
  125.  
  126.  
  127. /*
  128.  * Read a word from DSP CARD 3
  129.  */
  130. long ReadHostWord(void) {
  131.     long data;
  132.  
  133.     data  = ReadByte(RXH) << 16L;
  134.     data |= (unsigned)ReadByte(RXM) << 8L;
  135.     data |= (unsigned)ReadByte(RXL);
  136.  
  137.     return (data > 8388607L ? data - 16777216L : data);
  138. }
  139.  
  140.  
  141. /*
  142.  * Check that the given dump is ok
  143.  */
  144. void CheckMemDump(void) {
  145.     unsigned address;
  146.     int      i;
  147.  
  148.     /* wait preamble */
  149.     printf("Waiting sync pattern...");
  150.     while (!kbhit())
  151.     if ((ReadByte(ISR) & 0x01) && ReadHostWord() == 1313L)
  152.         break;
  153.  
  154.     printf(" found, checking data...\n");
  155.     for (i = p; i <= y; i++) {
  156.     printf("Space %s\n", memspaces[i].name);
  157.  
  158.     for (address = 0; address < memspaces[i].maxaddress; address++) {
  159.         while (!kbhit())
  160.         if (ReadByte(ISR) & 0x01) {
  161.             long data = ReadHostWord();
  162.  
  163.             if (memspaces[i].touched[address] && memspaces[i].data[address] != data)
  164.             printf("\t%s  %04X  %06lX (must have been %06lX)\n", memspaces[i].name, address, data, memspaces[i].data[address]);
  165.  
  166.             break;
  167.         }
  168.     }
  169.     }
  170. }
  171.