RGB332toNTSC.v 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Converts RGB332 video into NTSC composite 240p video
  3. */
  4. module RGB332toNTSC(
  5. input clk, //14.318MHz
  6. input clkColor, //114.5454MHz
  7. input [2:0] r,
  8. input [2:0] g,
  9. input [1:0] b,
  10. // counters for rendering
  11. output wire [11:0] hcount,
  12. output wire [11:0] vcount,
  13. output wire blank,
  14. output wire hs,
  15. output wire vs,
  16. output wire frameDrawn,
  17. output wire [7:0] composite // video output signal
  18. );
  19. wire clk14 = clk;
  20. wire clk114 = clkColor;
  21. //-------------------------
  22. //NTSC BASE SIGNAL
  23. //-------------------------
  24. wire [7:0] ntscBase;
  25. wire colorburst;
  26. wire hactive;
  27. wire vactive;
  28. wire active;
  29. assign active = hactive && vactive;
  30. assign blank = ~active;
  31. // Generate NTSC base signal
  32. NTSC ntsc(
  33. .clk (clk14),
  34. .ntscBase (ntscBase),
  35. .colorburst (colorburst),
  36. .hactive (hactive),
  37. .vactive (vactive),
  38. .hcount (hcount),
  39. .vcount (vcount),
  40. .hsync (hs),
  41. .vsync (vs),
  42. .frameDrawn (frameDrawn)
  43. );
  44. //-------------------------
  45. //COLOR PHASE
  46. //-------------------------
  47. wire colorPhase;
  48. wire burstPhase; // base phase used for color burst
  49. wire [4:0] phaseSelect;
  50. PhaseGen phasegen(
  51. .clk (clk114), //114.5454MHz
  52. .select (phaseSelect), // phase select
  53. .colorPhase (colorPhase), // phase of selected color
  54. .burstPhase (burstPhase) // phase of color burst
  55. );
  56. //-------------------------
  57. //COMBINER
  58. //-------------------------
  59. wire [7:0] luma;
  60. wire [7:0] colorAmplitude;
  61. RGBtoYPhaseAmpl rgb2yphampl(
  62. .rgb332({r,g,b}), // rrrgggbb
  63. .luma(luma),
  64. .phase(phaseSelect),
  65. .ampl(colorAmplitude)
  66. );
  67. // Apply clipping
  68. wire [7:0] ntscPlusColor;
  69. wire [7:0] ntscMinusColor;
  70. wire [7:0] ntscPlusColorClipped;
  71. wire [7:0] ntscMinusColorClipped;
  72. wire ntscPlusColorOverflow;
  73. wire ntscMinusColorOverflow;
  74. assign {ntscPlusColorOverflow, ntscPlusColor} = {1'b0, luma} + {1'b0, colorAmplitude};
  75. assign {ntscMinusColorOverflow, ntscMinusColor} = {1'b0, luma} - {1'b0, colorAmplitude};
  76. assign ntscPlusColorClipped = (ntscPlusColorOverflow) ? 8'd255 : ntscPlusColor;
  77. assign ntscMinusColorClipped = (ntscMinusColorOverflow) ? 8'd0 : ntscMinusColor;
  78. // Combine signal based on burst or color phase
  79. assign composite = (colorburst && burstPhase) ? ntscBase + 8'd36:
  80. (colorburst && ~burstPhase) ? ntscBase - 8'd36:
  81. (active && colorPhase) ? ntscPlusColorClipped:
  82. (active && ~colorPhase) ? ntscMinusColorClipped:
  83. ntscBase;
  84. endmodule