Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <limits.h>
  6. #include <stdint.h>
  7. #include <math.h>
  8.  
  9. #define LOG2W_REG_ADDR 0x00
  10. #define WIDTH_REG_ADDR 0x04
  11. #define LOG2H_REG_ADDR 0x08
  12. #define HEIGHT_REG_ADDR 0x0c
  13. #define CMD_REG_ADDR 0x10
  14. #define STATUS_REG_ADDR 0x14
  15.  
  16. typedef int32_t fixed_point_t;
  17. #define FIXED_POINT_FRACTIONAL_BITS 16
  18.  
  19. fixed_point_t double2int(double input)
  20. {
  21. if (input < 0) {
  22. printf("here\n");
  23. return -(fixed_point_t)(round(-input * (1 << FIXED_POINT_FRACTIONAL_BITS)));
  24. }
  25. return (fixed_point_t)(round(input * (1 << FIXED_POINT_FRACTIONAL_BITS)));
  26. }
  27.  
  28. char *int2bin(fixed_point_t a, char *buffer, int buf_size) {
  29. buffer += (buf_size - 1);
  30. for (int i = 31; i >= 0; i--) {
  31. *buffer-- = (a & 1) + '0';
  32. a >>= 1;
  33. }
  34. return buffer;
  35. }
  36.  
  37. int main(void)
  38. {
  39. //float image[]={4,4,4,4, 15.4736, 8.30876, 11.0691 , 10.1429 ,14.3318, 11.8821, 18.4516 ,5.40088 ,2.55646 ,2.90927, 4.63963 ,2.93407, 2.6015 ,19.5911, 9.71343, 5.299531,5.4736, 8.30876, 11.0691 , 10.1429 ,14.3318, 11.8821, 18.4516 ,5.40088 ,2.55646 ,2.90927, 4.63963 ,2.93407, 2.6015 ,19.5911, 9.71343, 5.29953
  40. //};
  41.  
  42. double image[]={
  43. 1.0, 0.0, 0.0, 0.0, // mat A
  44. 0.0, 0.0, 0.0, 0.0,
  45. 0.0, 0.0, 0.0, 0.0,
  46. 0.0, 0.0, 0.0, 0.0,
  47.  
  48. 1.0, 0.0, 0.0, 0.0, // matB
  49. 0.0, 0.0, 0.0, 0.0,
  50. 0.0, 0.0, 0.0, 0.0,
  51. 0.0, 0.0, 0.0, 0.0
  52. };
  53.  
  54. FILE* fp;
  55. int x,y;
  56. int log2w = 2, width = 4, log2h = 2, height = 4;
  57.  
  58. // TEST for int2bin and double2int
  59. /* char c[33]; */
  60. /* c[32] = '\0'; */
  61. /* int2bin(float_to_fixed(0.5),c, 32); */
  62. /* printf("%s\n", c); */
  63. /* int2bin(float_to_fixed(-0.5),c, 32); */
  64. /* printf("%s\n", c); */
  65. /* int2bin(float_to_fixed(1.0),c, 32); */
  66. /* printf("%s\n", c); */
  67.  
  68. /*
  69. ----------------------------
  70. extract height and width
  71. ----------------------------
  72. */
  73.  
  74. /* log2w = image[0]; */
  75. /* width = image[1]; */
  76. /* log2h = image[2]; */
  77. /* height = image[3]; */
  78.  
  79. //printf("Integer value is %d\n" , log2w);
  80. //printf("Integer value is %d\n" , width);
  81. //printf("Integer value is %d\n" , log2h);
  82. //printf("Integer value is %d\n", height);
  83.  
  84. /*
  85. ----------------------------
  86. write to IP
  87. ----------------------------
  88. */
  89.  
  90. fp = fopen("/dev/vga", "w"); // where to write to?
  91. if(fp == NULL)
  92. {
  93. printf("Cannot open /dev/vga for write\n");
  94. return -1;
  95. }
  96. fprintf(fp, "%d", LOG2W_ADDR_REG, log2w-1);
  97. fclose(fp);
  98.  
  99. fp = fopen("/dev/vga", "w"); // where to write to?
  100. if(fp == NULL)
  101. {
  102. printf("Cannot open /dev/vga for write\n");
  103. return -1;
  104. }
  105. fprintf(fp, "%d", WIDTH_ADDR_REG, width-1);
  106. fclose(fp);
  107.  
  108. fp = fopen("/dev/vga", "w"); // where to write to?
  109. if(fp == NULL)
  110. {
  111. printf("Cannot open /dev/vga for write\n");
  112. return -1;
  113. }
  114. fprintf(fp, "%d", LOG2H_ADDR_REG, log2h-1);
  115. fclose(fp);
  116.  
  117. fp = fopen("/dev/vga", "w"); // where to write to?
  118. if(fp == NULL)
  119. {
  120. printf("Cannot open /dev/vga for write\n");
  121. return -1;
  122. }
  123. fprintf(fp, "%d", HEIGHT_ADDR_REG, height-1);
  124. fclose(fp);
  125.  
  126. /*
  127. ----------------------------
  128. write to BRAM RE - values from file
  129. ----------------------------
  130. */
  131.  
  132. for (y=0; y<height; ++y)
  133. {
  134. for (x=0; x<width; ++x)
  135. {
  136. fp = fopen("/dev/vga", "w");
  137. if (fp == NULL)
  138. {
  139. printf("Cannot open /dev/vga for write\n");
  140. return -1;
  141. }
  142. fprintf(fp, "%d,%d,%#04x\n", x, y, double2int(image[y*width+x]) );
  143. fclose(fp);
  144. if (fp == NULL)
  145. {
  146. printf("Cannot close /dev/vga for write\n");
  147. return -1;
  148. }
  149. }
  150. }
  151.  
  152. /*
  153. ----------------------------
  154. write to BRAM IM - all zeros
  155. ----------------------------
  156. */
  157.  
  158. for (y=0; y<height; ++y)
  159. {
  160. for (x=0; x<width; ++x)
  161. {
  162. fp = fopen("/dev/vga", "w");
  163. if (fp == NULL)
  164. {
  165. printf("Cannot open /dev/vga for write\n");
  166. return -1;
  167. }
  168. fprintf(fp, "%d,%d,%#04x\n", x, y, 0 );
  169. fclose(fp);
  170. if (fp == NULL)
  171. {
  172. printf("Cannot close /dev/vga for write\n");
  173. return -1;
  174. }
  175. }
  176. }
  177.  
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement