NTSC.v 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * NTSC encoder
  3. */
  4. module NTSC(
  5. input clk, //14.31818MHz
  6. output wire [7:0] ntscBase,
  7. output wire colorburst,
  8. output wire hsync,
  9. output wire vsync,
  10. output wire hactive,
  11. output wire vactive,
  12. output reg [11:0] hcount,
  13. output reg [11:0] vcount,
  14. output wire frameDrawn
  15. );
  16. //assign hcountDiv2 = hcount >> 1; // divide by 2 to get 320 pixels instead of 640
  17. localparam HLINECYCLES = 910; // hline: 910 cycles for 63.555us (227.5 color burst cycles), 0.3V
  18. localparam VLINES = 263; // frame: 263 lines per frame, all the same, for 240P
  19. localparam HSYNCCYCLES = 67; // hsync: first 67 cycles of line, 0V
  20. localparam VSYNCCYCLES = 421; // vsync: first line: 421 cycles, apparently no hsync needed, 0V
  21. localparam HACTIVESTART = 196; // 0.35V Black to 1V White
  22. localparam HACTIVEDURATION = 640; //
  23. localparam VACTIVESTART = 40; //
  24. localparam VACTIVEDURATION = 200; //
  25. localparam COLORBURSTSTART = 70; // start a bit after hsync, 4.7+ (2.2/2) = 5.8, but a bit earlier, because extra cycle
  26. localparam COLORBURSTDURATION = 60; // 10 color burst cycles = 10*4 = 36
  27. //wire hsync;
  28. //wire vsync;
  29. assign hsync = hcount < HSYNCCYCLES;
  30. assign vsync = (vcount == 12'd0) && (hcount < VSYNCCYCLES);
  31. assign hactive = (hcount >= HACTIVESTART) && (hcount < (HACTIVESTART + HACTIVEDURATION));
  32. assign vactive = (vcount >= VACTIVESTART) && (vcount < (VACTIVESTART + VACTIVEDURATION));
  33. assign colorburst = !vsync && (hcount >= COLORBURSTSTART) && (hcount < (COLORBURSTSTART + COLORBURSTDURATION));
  34. assign ntscBase = (hsync || vsync) ? 8'd0: // low on sync
  35. //(hactive && vactive && vcount > 8'd200) ? 8'b11011001: // 255-38 for color modulation
  36. //(hactive) ? 8'd89: // officially: 8'd89 black level -> 0.35v
  37. 8'd72; // blank level -> 0.3v (77)
  38. //assign colorActive = vactive && hcount >= HACTIVESTART + 64 && hcount < HACTIVESTART + 576;
  39. always @(posedge clk)
  40. begin
  41. if (hcount != HLINECYCLES - 1)
  42. hcount <= hcount + 1'b1;
  43. else
  44. begin
  45. hcount <= 0;
  46. if (vcount != VLINES - 1)
  47. vcount <= vcount + 1'b1;
  48. else
  49. vcount <= 0;
  50. end
  51. end
  52. initial
  53. begin
  54. hcount = 12'd0;
  55. vcount = 12'd0;
  56. end
  57. assign frameDrawn = (vcount == 0 && hcount < 12'd32);
  58. endmodule