Guest User

Untitled

a guest
Jul 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. module ppu_if(rstn, clk, dot0, ack, wr, la, ld,
  2. ena, m2, rw, a, d, q, nmin, mdr,
  3. ca, cd, cq, cena, cm2, crw);
  4. input wire rstn, clk, dot0;
  5. output wire ack, wr;
  6. output wire [7:0] la;
  7. output wire [8:0] ld;
  8. input wire ena, m2, rw;
  9. input wire [2:0] a;
  10. input wire [7:0] d;
  11. output reg [7:0] q;
  12. output wire nmin;
  13. input wire [7:0] mdr;
  14. output wire [20:0] ca;
  15. output reg [7:0] cd;
  16. input wire [7:0] cq;
  17. output wire cena, cm2, crw;
  18.  
  19. wire [3:0] phi;
  20. wire [8:0] hc, vc;
  21. wire sync;
  22.  
  23. ppu_phasegen phase(.rstn(rstn), .clk(clk), .m2(m2), .phi(phi));
  24. ppu_counters hvcount(.rstn(rstn), .clk(clk), .phi(phi[2]), .dot0(dot0), .hc(hc), .vc(vc), .sync(sync));
  25.  
  26. assign cm2 = phi[1];
  27.  
  28. reg ff, vbl, hit, over, blank;
  29. reg [7:0] ctrl, mask, oamaddr, buf2007;
  30. reg [2:0] xfine;
  31. reg [14:0] t;
  32. reg [14:0] v;
  33. reg clrvbl, incv, inco, rstv, rd2007, wr2007;
  34.  
  35. wire rendering = |mask[4:3] & ~blank;
  36.  
  37. always @(posedge clk)
  38. if (~sync)
  39. ; // ignore everything until sync triggers
  40. else if (phi[0])
  41. begin
  42. // blank/vbl, set at start of vblank, cleared after
  43. if (~|hc)
  44. if (~|vc)
  45. blank <= 1'b0;
  46. else if (vc == 9'd242)
  47. begin
  48. blank <= 1'b1;
  49. if (clrvbl)
  50. vbl <= 1'b0;
  51. else
  52. vbl <= 1'b1;
  53. end
  54.  
  55. end
  56. else if (phi[1])
  57. ;
  58. else if (phi[2])
  59. ;
  60. else if (phi[3])
  61. begin
  62. clrvbl <= 1'b0;
  63. rstv <= 1'b0;
  64. rd2007 <= 1'b0;
  65. wr2007 <= 1'b0;
  66. inco <= 1'b0;
  67. incv <= 1'b0;
  68.  
  69. if (m2 & ena)
  70. if (rw)
  71. if (a == 3'h2)
  72. begin
  73. q <= { vbl, hit, over, mdr[4:0] };
  74. ff <= 1'b0;
  75. clrvbl <= 1'b1;
  76. end
  77. else if (a == 3'h4)
  78. ; // oamdat
  79. else if (a == 3'h7)
  80. begin
  81. q <= buf2007;
  82. rd2007 <= 1'b1;
  83. end
  84. else
  85. q <= mdr;
  86. else
  87. case (a)
  88. 3'h0: begin ctrl <= d; t[11:10] <= d[1:0]; end
  89. 3'h1: mask <= d;
  90. 3'h3: oamaddr <= d;
  91. 3'h4: inco <= 1'b1; // write to oam, inc
  92. 3'h5:
  93. begin
  94. if (ff) { t[9:5], t[14:12] } <= d;
  95. else { t[4:0], xfine } <= d;
  96. ff <= ~ff;
  97. end
  98. 3'h6:
  99. begin
  100. if (ff) begin t[7:0] <= d; rstv <= 1'b1; end
  101. else t[14:8] <= { 1'b0, d[5:0] };
  102. end
  103. 3'h7:
  104. begin
  105. wr2007 <= 1'b1;
  106. cd <= d;
  107. end
  108. endcase // case(a)
  109. else
  110. begin
  111. if (inco)
  112. oamaddr <= oamaddr + 1'b1;
  113. if (incv)
  114. ;
  115. end // else: !if(a == 3'h7)
  116. end // if (phi[3])
  117.  
  118. always @(posedge clk)
  119. if (m2)
  120. begin
  121. clrvbl <= 1'b0;
  122. rstv <= 1'b0;
  123.  
  124. if (ena & rw)
  125. begin
  126. if (a == 3'h2)
  127. begin
  128. q <= { vbl, hit, over, mdr[4:0] };
  129. clrvbl <= 1'b1;
  130. ff <= 1'b0;
  131. end
  132. else if (a == 3'h4)
  133. begin
  134. ; // q = oamdat
  135. inco <= 1'b1;
  136. end
  137. else if (a == 3'h7)
  138. begin
  139. q <= buf2007;
  140. rd2007 <= 1'b1;
  141. incv <= 1'b1;
  142. end
  143. else
  144. q <= mdr;
  145. end // if (rw)
  146. else if (ena & ~rw)
  147. begin
  148. case (a)
  149. 3'h0: begin ctrl <= d; t[11:10] <= d[1:0]; end
  150. 3'h1: mask <= d;
  151. 3'h3: oamaddr <= d;
  152. 3'h4: ; // write to oam, inc
  153. 3'h5:
  154. begin
  155. if (ff) { t[9:5], t[14:12] } <= d;
  156. else { t[4:0], xfine } <= d;
  157. ff <= ~ff;
  158. end
  159. 3'h6:
  160. begin
  161. if (ff) begin t[7:0] <= d; rstv <= 1'b1; end
  162. else t[14:8] <= { 1'b0, d[5:0] };
  163. end
  164. 3'h7: ; // write to vram, inc
  165. endcase
  166. end // if (ena & ~rw)
  167. end // if (m2)
  168.  
  169. // ack tells the buffer manager to swap the PPU over to the other one, and that the
  170. // line is ready for scanout
  171. always @(posedge clk)
  172. if (~rstn)
  173. ack <= 1'b0;
  174. else if (phi[2] & hc == 9'd256)
  175. ack <= 1'b1;
  176. else
  177. ack <= 1'b0;
  178.  
  179. always @(posedge clk)
  180. if (~rstn)
  181. begin
  182. la <= 8'h0;
  183. ld <= 9'h0;
  184. wr <= 1'b0;
  185. end
  186. else if (phi[2] & hc < 9'd256)
  187. begin
  188. la <= hc[7:0];
  189. ld <= hs; // output of shift regs
  190. wr <= 1'b1;
  191. end
  192.  
  193. endmodule // ppu_if
Add Comment
Please, Sign In to add comment