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

  1. /* Devpac DSP example code MATMULT5.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 "MATMULT5.B56"
  16. typedef char DSP_WORD[3];
  17. typedef float MATRIX[N][N];
  18.  
  19. static int MulAbility; 
  20. static int MultHandle;
  21.  
  22. int InitMult(void)
  23. {
  24. DSP_WORD *dsp_prog;
  25. FILE * fp;
  26. long prog_size;
  27. MultHandle=Dsp_InqSubrAbility(MulAbility);
  28. if (MultHandle==0)
  29.     {
  30.         /* Now open the DSP file */
  31.         fp=fopen(DSP_CODE_FILE,"rb");
  32.         if (fp==NULL)
  33.             {
  34.                 printf("Can't open file " DSP_CODE_FILE "\n");
  35.                 Dsp_Unlock();
  36.                 return 10;
  37.             }
  38.     
  39.         /* Now read the DSP file into dsp_prog with the size in dsp words in prog_size */    
  40.         fseek(fp,0,SEEK_END);
  41.         prog_size=ftell(fp)/sizeof(DSP_WORD);
  42.         fseek(fp,0,SEEK_SET);
  43.         dsp_prog=malloc(prog_size*sizeof(DSP_WORD));
  44.         fread(dsp_prog,sizeof(DSP_WORD),prog_size,fp);
  45.         fclose(fp);
  46.  
  47.         MultHandle=Dsp_LoadSubroutine(dsp_prog,prog_size,MulAbility);
  48.         if (MultHandle==0)
  49.         {
  50.             printf("Can't load subroutine\n");
  51.             return 10;
  52.         }
  53.     }
  54. return 0;
  55. }
  56. void showmat(MATRIX c)
  57. {
  58. int i,j;
  59.     for    (j=0;j<N;j++)
  60.     {
  61.         for    (i=0;i<N;i++)
  62.             printf("%f\t",c[i][j]);
  63.         printf("\n");
  64.     }
  65.     printf("\n");
  66. }
  67.  
  68. /* multiply a by b giving c - returns non-zero if error */
  69. int mult(MATRIX a,MATRIX b,MATRIX c)
  70. {
  71. long a1[N][N],b1[N][N],c1[N][N*2];
  72. int i,j;
  73.  
  74. if (InitMult()) return 10;
  75. /* First we need to convert the values to fixed point */
  76. for    (i=0;i<N;i++)
  77.         for    (j=0;j<N;j++)
  78.             {
  79.                 a1[i][j]= 0x800000*a[i][j];
  80.                 b1[i][j]= 0x800000*b[i][j];
  81.             }
  82.  
  83.     if(Dsp_RunSubroutine(MultHandle))
  84.         {
  85.             printf("Can't run subroutine\n");
  86.             return 10; 
  87.         }
  88. /* send the data */
  89.     Dsp_BlkUnpacked(&a1[0][0],N*N,0,0);
  90.     Dsp_BlkUnpacked(&b1[0][0],N*N,0,0);
  91.  
  92. /* get the results back one at a time */
  93.     for    (i=0;i<N;i++)
  94.         for    (j=0;j<N;j++)
  95.             {
  96.             Dsp_BlkUnpacked(0,0,&c1[i][j*2],2);
  97.             c[i][j]=c1[i][j*2]*2 +(float)(c1[i][j*2+1])/0x800000;
  98.             }
  99.     
  100.     return 0;
  101. }
  102. int main(void)
  103. {
  104. MATRIX a,b,c;
  105. int i,j;
  106.  
  107. MulAbility=Dsp_RequestUniqueAbility();
  108.  
  109. for    (i=0;i<N;i++)
  110.         for    (j=0;j<N;j++)
  111.             {
  112.                 a[i][j]=  (float)(rand())/(RAND_MAX+1);
  113.                 b[i][j]=  (float)(rand())/(RAND_MAX+1);
  114.             }
  115.  
  116.     showmat(a);
  117.     showmat(b);
  118.  
  119.     if(mult(a,b,c)) 
  120.         {
  121.             printf("multiply failed\n");
  122.             return 10;
  123.         }
  124.     
  125.     showmat(c);
  126.     return 0;    
  127. }
  128.