Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. // This is a Morse code encoder circuit. It displays the Morse code for
  2. // letters A to H on LEDR[0]. The letter to be displayed is selected using
  3. // SW[3..0], using A = 000, B = 001, ..., H = 111.
  4. module part3 (SW, CLOCK_50, KEY, LEDR);
  5. /******************************************************************/
  6. /**** PARAMETER DECLARATIONS ****/
  7. /******************************************************************/
  8. // SW switch patterns, Morse codes, and code lengths are defined below (in the Morse
  9. // code, 0 = dot, 1 = dash)
  10. parameter A_SW = 3'b000, A_MORSE = 4'b0010, A_LENGTH = 3'd2; /* .- */
  11. parameter B_SW = 3'b001, B_MORSE = 4'b0001, B_LENGTH = 3'd4; /* -... */
  12. parameter C_SW = 3'b010, C_MORSE = 4'b0101, C_LENGTH = 3'd4; /* -.-. */
  13. parameter D_SW = 3'b011, D_MORSE = 4'b0001, D_LENGTH = 3'd3; /* -.. */
  14. parameter E_SW = 3'b100, E_MORSE = 4'b0000, E_LENGTH = 3'd1; /* . */
  15. parameter F_SW = 3'b101, F_MORSE = 4'b0100, F_LENGTH = 3'd4; /* ..-. */
  16. parameter G_SW = 3'b110, G_MORSE = 4'b0011, G_LENGTH = 3'd3; /* --. */
  17. parameter H_SW = 3'b111, H_MORSE = 4'b0000, H_LENGTH = 3'd4; /* .... */
  18.  
  19. parameter s_WAIT_SEND = 3'b000, s_WAIT_BLANK = 3'b001, s_SEND_DOT = 3'b010,
  20. s_SEND_DASH_1 = 3'b011, s_SEND_DASH_2 = 3'b100, s_SEND_DASH_3 = 3'b101,
  21. s_RELEASE_SEND = 3'b110;
  22.  
  23. /******************************************************************/
  24. /**** PORT DECLARATIONS ****/
  25. /******************************************************************/
  26. input [2:0] SW;
  27. input [1:0] KEY;
  28. input CLOCK_50;
  29. output [9:0] LEDR;
  30.  
  31. /******************************************************************/
  32. /**** LOCAL WIRE DECLARATIONS ****/
  33. /******************************************************************/
  34. wire Clock, Resetn, go, half_sec_enable, load_regs, shift_and_count, light_on;
  35. reg [3:0] morse_code;
  36. reg [2:0] morse_length;
  37. reg [3:0] send_data;
  38. reg [2:0] data_size;
  39. wire [1:0] pulse_cycle;
  40. reg [3:0] y_Q, Y_D;
  41.  
  42. /******************************************************************/
  43. /**** IMPLEMENTATION ****/
  44. /******************************************************************/
  45. assign Clock = CLOCK_50;
  46. assign Resetn = KEY[0];
  47. assign go = ~KEY[1];
  48.  
  49. // FSM State Table
  50. always @(go, y_Q, send_data, data_size, half_sec_enable)
  51. begin: state_table
  52. case (y_Q)
  53. s_WAIT_SEND:
  54. if (go) Y_D = s_WAIT_BLANK;
  55. else Y_D = s_WAIT_SEND;
  56. s_WAIT_BLANK: // sync with the half-second pulses
  57. if (!half_sec_enable)
  58. Y_D = s_WAIT_BLANK;
  59. else if (send_data[0] == 1'b0)
  60. Y_D = s_SEND_DOT;
  61. else
  62. Y_D = s_SEND_DASH_1;
  63. s_SEND_DOT: // wait here for one half-second period
  64. if (!half_sec_enable)
  65. Y_D = s_SEND_DOT;
  66. else if (data_size == 'd1) // check if we are done with this letter
  67. Y_D = s_RELEASE_SEND;
  68. else
  69. Y_D = s_WAIT_BLANK;
  70. s_SEND_DASH_1: // wait for three half-second periods
  71. if (!half_sec_enable)
  72. Y_D = s_SEND_DASH_1;
  73. else
  74. Y_D = s_SEND_DASH_2;
  75. s_SEND_DASH_2: // wait for two more half-second periods
  76. if (!half_sec_enable)
  77. Y_D = s_SEND_DASH_2;
  78. else
  79. Y_D = s_SEND_DASH_3;
  80. s_SEND_DASH_3: // wait for one more half-second period
  81. if (!half_sec_enable)
  82. Y_D = s_SEND_DASH_3;
  83. else if (data_size == 'd1) // check if we are done with this letter
  84. Y_D = s_RELEASE_SEND;
  85. else
  86. Y_D = s_WAIT_BLANK;
  87. s_RELEASE_SEND:
  88. if (~go) Y_D = s_WAIT_SEND;
  89. else Y_D = s_RELEASE_SEND;
  90.  
  91. default: Y_D = 3'bxxx;
  92. endcase
  93. end // state_table
  94.  
  95. // FSM State flip-flops
  96. always @(posedge Clock)
  97. if (Resetn == 1'b0) // synchronous clear
  98. y_Q <= s_WAIT_SEND;
  99. else
  100. y_Q <= Y_D;
  101.  
  102. // FSM outputs
  103. // turn on the Morse code light in the states below
  104. assign light_on = ( (y_Q == s_SEND_DOT) | (y_Q == s_SEND_DASH_1) |
  105. (y_Q == s_SEND_DASH_2) | (y_Q == s_SEND_DASH_3) );
  106. // specify when to load the Morse code into the shift register, and length into the counter
  107. assign load_regs = (y_Q == s_WAIT_SEND) & go;
  108. // specify when to shift the Morse code bits and decrement the length counter
  109. assign shift_and_count = ((y_Q == s_SEND_DOT) | (y_Q == s_SEND_DASH_3)) & half_sec_enable;
  110.  
  111. /* Create an enable signal that is asserted once every 0.5 of a second. */
  112. modulo_counter half_sec( .Clock(CLOCK_50), .Resetn(Resetn), .rollover(half_sec_enable) );
  113. defparam half_sec.n = 25;
  114. defparam half_sec.k = 25000000;
  115.  
  116. /* Letter selection */
  117. always @(*)
  118. case (SW)
  119. A_SW: begin morse_code = A_MORSE; morse_length = A_LENGTH; end
  120. B_SW: begin morse_code = B_MORSE; morse_length = B_LENGTH; end
  121. C_SW: begin morse_code = C_MORSE; morse_length = C_LENGTH; end
  122. D_SW: begin morse_code = D_MORSE; morse_length = D_LENGTH; end
  123. E_SW: begin morse_code = E_MORSE; morse_length = E_LENGTH; end
  124. F_SW: begin morse_code = F_MORSE; morse_length = F_LENGTH; end
  125. G_SW: begin morse_code = G_MORSE; morse_length = G_LENGTH; end
  126. H_SW: begin morse_code = H_MORSE; morse_length = H_LENGTH; end
  127. endcase
  128.  
  129. /* Store the Morse code to be sent in a shift register, and its length in a counter */
  130. always@(posedge CLOCK_50)
  131. begin
  132. if (~Resetn)
  133. begin
  134. send_data <= 'd0;
  135. data_size <= 'd0;
  136. end
  137. else
  138. if (load_regs)
  139. begin
  140. send_data <= morse_code;
  141. data_size <= morse_length;
  142. end
  143. else if (shift_and_count) // shift and decrement when appropriate
  144. begin
  145. send_data[2:0] <= send_data[3:1];
  146. send_data[3] <= 1'b0;
  147. data_size <= data_size - 1'b1;
  148. end
  149. end
  150.  
  151. assign LEDR[0] = light_on;
  152. assign LEDR[9:1] = 9'b0;
  153. endmodule
  154.  
  155. module modulo_counter(Clock, Resetn, rollover);
  156. /******************************************************************/
  157. /**** PARAMETER DECLARATIONS ****/
  158. /******************************************************************/
  159. parameter n = 4;
  160. parameter k = 16;
  161.  
  162. /******************************************************************/
  163. /**** PORT DECLARATIONS ****/
  164. /******************************************************************/
  165. input Clock, Resetn;
  166. output rollover;
  167. reg [n-1:0] Q;
  168.  
  169. /******************************************************************/
  170. /**** IMPLEMENTATION ****/
  171. /******************************************************************/
  172. always@(posedge Clock)
  173. begin
  174. if (!Resetn)
  175. Q <= 'd0;
  176. else if (Q == k-1)
  177. Q <= 'd0;
  178. else
  179. Q <= Q + 1'b1;
  180. end
  181.  
  182. assign rollover = (Q == k-1);
  183. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement