Advertisement
andreahmed

Untitled

Mar 18th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module line_drawer(x1, y1, x2, y2, VGA_SYNC, VGA_CLK,
  2. data_reg, reset, color, command,
  3. available,
  4. my_id,
  5. id_req,
  6. xAddr, yAddr,
  7. write_clk,
  8. is_write_finished
  9. );
  10. input wire signed [10:0] x1, y1, x2, y2;
  11. input wire VGA_SYNC, VGA_CLK;
  12. output reg [15:0] data_reg; //memory data register for SRAM
  13. input wire reset;
  14. input wire [15:0] color;
  15. input wire [1:0] command;
  16. output wire available;
  17. input wire [3:0] my_id, id_req;
  18. output reg write_clk;
  19. input wire is_write_finished;
  20. output reg [9:0] xAddr;
  21. output reg [8:0] yAddr;
  22. //state machine
  23. reg [2:0] state, next_state;
  24. // absolute values of dx, dy, e
  25. wire signed [10:0] absdx, absdy;
  26. assign absdx = (x2>x1) ? (x2-x1) : (x1-x2);
  27. assign absdy = (y2>y1) ? (y2-y1) : (y1-y2);
  28. // initial value of e should be negative
  29. wire signed [10:0] nege;
  30. assign nege = ((absdy - absdx) < 0) ? (absdy-absdx) : (absdx-absdy);
  31. // whether or not x is the DA
  32. wire xDA;
  33. assign xDA = (absdx > absdy);
  34. // signs of dx and dy (0 = positive, 1 = negative)
  35. wire dxsign, dysign;
  36. assign dxsign = ((x2-x1) < 0);
  37. assign dysign = ((y2-y1) < 0);
  38. // value to increment e by
  39. wire signed [10:0] einc;
  40. assign einc = (xDA) ? (absdy) : (absdx);
  41. // value to decrement e by
  42. wire signed [10:0] edec;
  43. assign edec = (xDA) ? (absdx) : (absdy);
  44. // value to increment i and j by
  45. wire signed [10:0] iinc, jinc;
  46. assign iinc = (xDA) ? (dxsign ? (-1) : (1)) : (dysign ? (-1): (1));
  47. assign jinc = (xDA) ? (dysign ? (-1) : (1)) : (dxsign ? (-1): (1));
  48. // finish indicates when line is finished drawing
  49. wire finish, xfinish, yfinish;
  50. // if x-increment is negative, stop when i is less than x2, etc.
  51. assign xfinish = (dxsign) ? (i < x2) : (i > x2);
  52. // if y-increment is negative, stop when i is less than x2, etc.
  53. assign yfinish = (dysign) ? (i < y2) : (i > y2);
  54. // if DA is x, check x for done drawing, else check y
  55. assign finish = (xDA) ? (xfinish) : (yfinish);
  56. reg available_reg;
  57. assign available = available_reg;
  58. // for Bresenham's Algorithm
  59. reg signed [10:0] dy, dx, e, j, i;
  60. //state names
  61. parameter
  62. setup = 3'd0, // initialize Bresenham's algorithm
  63. draw_line = 3'd1, // draw the line
  64. ignore = 3'd3,
  65. halt = 3'd2,
  66. write_state = 3'd4; // wait for next drawline command
  67. always @ (posedge VGA_CLK)
  68. begin
  69. if (reset) //synch reset assumes KEY0 is held down 1/60 second
  70. begin
  71. data_reg <= 16'b0; //write all zeros (black)
  72. state <= halt;
  73. available_reg <= 1'b0;
  74. write_clk <= 1'b0;
  75. next_state <= write_state;
  76. end
  77. //modify display during sync
  78. else if (VGA_SYNC)
  79. begin
  80. case(state)
  81. write_state: begin
  82. if(is_write_finished) begin
  83. state <= next_state;
  84. write_clk <= 1'b0;
  85. end
  86. end
  87. //setup line drawing
  88. setup:
  89. begin
  90. available_reg <= 1'b0; //indicate that module is in use
  91. dy <= absdy;
  92. dx <= absdx;
  93. e <= nege;
  94. // i always gets increments
  95. // j gets incremented when e>0
  96. if (xDA)
  97. begin
  98. i <= x1;
  99. j <= y1;
  100. end
  101. else
  102. begin
  103. i <= y1;
  104. j <= x1;
  105. end
  106. state <= draw_line;
  107. end
  108. //draw the line
  109. draw_line:
  110. begin
  111. if (finish)
  112. begin
  113. state <= halt;
  114. //we <= 1'b1;
  115. end
  116. else
  117. begin
  118. //we <= 1'b0;
  119. // if x is DA, i is x, y is j
  120. if (xDA)
  121. begin
  122. data_reg <= color;
  123. xAddr <= i[9:0];
  124. yAddr <= j[8:0];
  125. next_state <= draw_line;
  126. write_clk <= 1'b1;
  127. state <= write_state;
  128. end
  129. // if y is DA, j is x, y is i
  130. else
  131. begin
  132. data_reg <= color;
  133. xAddr <= j[9:0];
  134. yAddr <= i[8:0];
  135. next_state <= draw_line;
  136. write_clk <= 1'b1;
  137. state <= write_state;
  138. end
  139. if (e >= 0)
  140. begin
  141. j <= j + jinc;
  142. e <= e - edec + einc;
  143. i <= i + iinc;
  144. end
  145. else
  146. begin
  147. i <= i + iinc;
  148. e <= e + einc;
  149. end
  150. end
  151. end
  152. ignore: begin
  153. available_reg <= 1'b1;
  154. if(~command[1]) begin
  155. state <= halt;
  156. end
  157. end
  158. //wait for next draw line command
  159. halt:
  160. begin
  161. if (command[1] && (my_id == id_req))
  162. begin
  163. state <= setup;
  164. available_reg <= 1'b0;
  165. end
  166. //we <= 1'b1;
  167. if(~command[1])available_reg <= 1'b1; //indicate that module is free for use
  168. end
  169. default:
  170. begin
  171. available_reg <= 1'b0;
  172. end
  173. endcase
  174. end
  175. else
  176. begin
  177. end
  178. end
  179. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement