home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_10 / DEVPAC56.ZIP / MATMULT4.C < prev    next >
C/C++ Source or Header  |  1993-12-20  |  2KB  |  122 lines

  1. /* Devpac DSP example code MATMULT4.C */
  2. /* for Lattice C                      */
  3. /* (c) Copyright HiSoft 1993          */
  4. /* All rights reserved                */
  5.  
  6. /* Note that this uses different data to the BASIC examples as
  7.    the random number algorithms used by the languages are different
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <osbind.h>
  13.  
  14. #define N 4
  15. #define DSP_CODE_FILE "MATMULT4.P56"
  16. typedef char DSP_WORD[3];
  17. typedef float MATRIX[N][N];
  18.  
  19.  
  20. void showmat(MATRIX c)
  21. {
  22. int i,j;
  23.     for    (j=0;j<N;j++)
  24.     {
  25.         for    (i=0;i<N;i++)
  26.             printf("%f\t",c[i][j]);
  27.         printf("\n");
  28.     }
  29.     printf("\n");
  30. }
  31.  
  32. /* multiply a by b giving c - returns non-zero if error */
  33. int mult(MATRIX a,MATRIX b,MATRIX c)
  34. {
  35. DSP_WORD *dsp_prog;
  36. FILE * fp;
  37. long prog_size;
  38. int abil;
  39. long a1[N][N],b1[N][N],c1[N][N*2];
  40. int i,j;
  41.  
  42. /* First we need to convert the values to fixed point */
  43. for    (i=0;i<N;i++)
  44.         for    (j=0;j<N;j++)
  45.             {
  46.                 a1[i][j]= 0x800000*a[i][j];
  47.                 b1[i][j]= 0x800000*b[i][j];
  48.             }
  49.  
  50.  
  51.     if (Dsp_Lock())
  52.         {
  53.             printf("DSP already in use\n");
  54.             return 10; 
  55.         }
  56.     
  57.     if (Dsp_Reserve(0x40+N*N,0x1000+N*N*3))
  58.         {
  59.             printf("Can't reserve enough DSP RAM\n");
  60.             Dsp_Unlock();
  61.             return 10;
  62.         }
  63.  
  64. /* Now open the DSP file */
  65.     fp=fopen(DSP_CODE_FILE,"rb");
  66.     if (fp==NULL)
  67.         {
  68.             printf("Can't open file " DSP_CODE_FILE "\n");
  69.             Dsp_Unlock();
  70.             return 10;
  71.         }
  72.     
  73. /* Now read the DSP file into dsp_prog with the size in dsp words in prog_size */    
  74.     fseek(fp,0,SEEK_END);
  75.     prog_size=ftell(fp)/sizeof(DSP_WORD);
  76.     fseek(fp,0,SEEK_SET);
  77.     dsp_prog=malloc(prog_size*sizeof(DSP_WORD));
  78.     fread(dsp_prog,sizeof(DSP_WORD),prog_size,fp);
  79.     fclose(fp);
  80.  
  81.     abil=Dsp_RequestUniqueAbility();
  82.     Dsp_ExecProg(dsp_prog,prog_size,abil);
  83. /* send the data */
  84.     Dsp_BlkUnpacked(&a1[0][0],N*N,0,0);
  85.     Dsp_BlkUnpacked(&b1[0][0],N*N,0,0);
  86.  
  87.     Dsp_BlkUnpacked(0,0,&c1[0][0],N*N*2);
  88.  
  89.     for    (i=0;i<N;i++)
  90.         for    (j=0;j<N;j++)
  91.             {
  92.             c[i][j]=c1[i][j*2]*2 +(float)(c1[i][j*2+1])/0x800000;
  93.             }
  94.     
  95.     Dsp_Unlock();
  96.     return 0;
  97. }
  98. int main(void)
  99. {
  100. MATRIX a,b,c;
  101. int i,j;
  102.  
  103. for    (i=0;i<N;i++)
  104.         for    (j=0;j<N;j++)
  105.             {
  106.                 a[i][j]=  (float)(rand())/(RAND_MAX+1);
  107.                 b[i][j]=  (float)(rand())/(RAND_MAX+1);
  108.             }
  109.  
  110.     showmat(a);
  111.     showmat(b);
  112.  
  113.     if(mult(a,b,c)) 
  114.         {
  115.             printf("multiply failed\n");
  116.             return 10;
  117.         }
  118.     
  119.     showmat(c);
  120.     return 0;    
  121. }
  122.