Advertisement
Guest User

Untitled

a guest
Aug 24th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module test (
  2.     // 48 MHz clock
  3.     input clk,
  4.     input [3:0] sw, // unused
  5.  
  6.     output ds_dp, ds_g, ds_c, ds_d,
  7.     output ds_a, ds_b, ds_e, ds_f,
  8.     output [3:0] ds_en,
  9.  
  10.     output [4:0] vga_red,
  11.     output [5:0] vga_green,
  12.     output [4:0] vga_blue,
  13.     output vga_hsync, vga_vsync,
  14.  
  15.     inout ps2_data, ps2_clk,
  16.  
  17.     output bell // unused
  18. );
  19.  
  20. // UNCOMMENTING THIS BREAKS PS/2
  21.  
  22. /*
  23. // PLL
  24. wire pixel_clk;
  25.  
  26. pll pll (
  27.     .inclk0(clk),
  28.     .c0(pixel_clk)
  29. );
  30.  
  31. // VGA (outputs current x, y and active video bit)
  32. wire [9:0] vga_x, vga_y;
  33. wire vga_active;
  34. vga vga (
  35.     .pixel_clk(pixel_clk),
  36.     .x(vga_x),
  37.     .y(vga_y),
  38.     .active(vga_active),
  39.     .hsync(vga_hsync),
  40.     .vsync(vga_vsync)
  41. );
  42.  
  43. // Grapher (outputs pixel colors)
  44.  
  45. grapher grapher (
  46.     .pixel_clk(pixel_clk),
  47.     .x(vga_x),
  48.     .y(vga_y),
  49.     .active(vga_active),
  50.     .red(vga_red),
  51.     .green(vga_green),
  52.     .blue(vga_blue)
  53. );
  54. */
  55.  
  56. // PS/2
  57.  
  58. wire [7:0] ps2_cmd_out;
  59. wire ps2_cmd_out_ready;
  60. wire [7:0] ps2_cmd_in;
  61. wire ps2_cmd_in_ready;
  62. wire ps2_busy;
  63.  
  64. ps2 ps2 (
  65.     .clk(clk),
  66.     .ps2_clk(ps2_clk),
  67.     .ps2_data(ps2_data),
  68.     .cmd_out(ps2_cmd_out),
  69.     .cmd_out_ready(ps2_cmd_out_ready),
  70.     .cmd_in(ps2_cmd_in),
  71.     .cmd_in_ready(ps2_cmd_in_ready),
  72.     .busy(ps2_busy)
  73. );
  74.  
  75. // PS/2 mouse
  76. /*
  77. wire [7:0] ps2_mouse_x;
  78. wire [7:0] ps2_mouse_y;
  79.  
  80. ps2_mouse ps2_mouse (
  81.     .clk(clk),
  82.     .cmd_in(ps2_cmd_in),
  83.     .cmd_in_ready(ps2_cmd_in_ready),
  84.     .busy(ps2_busy),
  85.     .cmd_out(ps2_cmd_out),
  86.     .cmd_out_ready(ps2_cmd_out_ready),
  87.     .x(ps2_mouse_x),
  88.     .y(ps2_mouse_y)
  89. );
  90.  
  91. reg [7:0] mouse_x = 0;
  92. reg [7:0] mouse_y = 0;
  93.  
  94. always @(posedge clk) begin
  95.     mouse_x <= mouse_x + ps2_mouse_x;
  96.     mouse_y <= mouse_y + ps2_mouse_y;
  97. end
  98. */
  99.  
  100. // seg
  101.  
  102. // displaying current command on 7 seg
  103. wire [15:0] seg_num = ps2_cmd_in; //{mouse_x, mouse_y};
  104.  
  105. seg seg (
  106.     .clk(clk),
  107.     .num(seg_num),
  108.     .segs({ds_a, ds_b, ds_c, ds_d, ds_e, ds_f, ds_g, ds_dp}),
  109.     .mask(ds_en)
  110. );
  111.  
  112. endmodule
  113.  
  114. module ps2 (
  115.     input clk,
  116.  
  117.     inout ps2_clk,
  118.     inout ps2_data,
  119.  
  120.     input [7:0] cmd_out,
  121.     input cmd_out_ready, // active for 1 clock cycle
  122.  
  123.     output reg [7:0] cmd_in,
  124.     output reg cmd_in_ready, // active for 1 clock cycle
  125.  
  126.     output reg busy // 1 if either sending or receiving
  127. );
  128.  
  129. initial begin
  130.     busy <= 0;
  131.     cmd_in <= 0;
  132.     cmd_in_ready <= 0;
  133. end
  134.  
  135. reg ps2_clk_enable = 0;
  136. assign ps2_clk = ps2_clk_enable ? 1'b0 : 1'bZ;
  137.  
  138. wire ps2_clk_in; // input from ps2_clk, synchronized
  139. sync clk_sync (
  140.     .clk(clk),
  141.     .in(ps2_clk),
  142.     .out(ps2_clk_in)
  143. );
  144.  
  145. reg ps2_data_enable = 0;
  146. reg ps2_data_out = 0;
  147. assign ps2_data = ps2_data_enable ? ps2_data_out : 1'bZ;
  148.  
  149. wire ps2_data_in; // input from ps2_data, synchronized
  150. sync data_sync (
  151.     .clk(clk),
  152.     .in(ps2_data),
  153.     .out(ps2_data_in)
  154. );
  155.  
  156. /*
  157. 0 -> inactive, waiting for new input
  158.  
  159. send to ps2 states
  160. 1 -> start inhibit ps2 clock
  161. 2 -> inhibit ps2 clock, and release ps2_clk
  162. 3 -> send data, parity bit
  163. 4 -> send stop bit
  164. 5 -> wait for ack bit, goto inactive state 0
  165.  
  166. receive from ps2 states
  167. 6 -> receive data, receive parity
  168. 7 -> receive stop
  169. 8 -> cmd_in_ready set to high
  170. 9 -> cmd_in_ready set to low
  171. */
  172.  
  173. reg [3:0] state = 0;
  174.  
  175. reg [7:0] cmd = 0; // internal buffer for the command to be sent
  176. reg [12:0] counter = 0; // used for inhibit and counting bits for shiftregs
  177. reg cycle = 0; // used for edge detection
  178. reg parity = 0; // parity bit, what else to say
  179.  
  180. always @(posedge clk) begin
  181.     // state reacting to system clock
  182.     case (state)
  183.         0: if (cmd_out_ready) begin
  184.             cmd <= cmd_out;
  185.             busy <= 1;
  186.  
  187.             state <= 1;
  188.         end
  189.         1: begin
  190.             ps2_clk_enable <= 1;
  191.  
  192.             state <= 2;
  193.         end
  194.         2: begin
  195.             counter <= counter + 1'b1;
  196.  
  197.             if (counter == 5000) begin
  198.                 // set counter to count bits
  199.                 counter <= 8;
  200.                 ps2_clk_enable <= 0;
  201.                 ps2_data_out <= 0;
  202.                 ps2_data_enable <= 1;
  203.  
  204.                 state <= 3;
  205.             end
  206.         end
  207.         8: begin
  208.             cmd_in_ready <= 1;
  209.  
  210.             state <= 9;
  211.         end
  212.         9: begin
  213.             cmd_in_ready <= 0;
  214.  
  215.             state <= 0;
  216.         end
  217.     endcase
  218.  
  219.     if (!ps2_clk_in) begin
  220.         if (cycle) begin
  221.             cycle <= 0;
  222.  
  223.             // state reacting to ps2 clock
  224.             case (state)
  225.                 // receive check
  226.                 0: if (!busy) begin
  227.                     busy <= 1;
  228.                     // counter to count bits
  229.                     counter <= 8;
  230.                
  231.                     state <= 6;
  232.                 end
  233.  
  234.                 // send states
  235.                 3: if (counter == 0) begin
  236.                     // parity bit
  237.                     ps2_data_out <= ~parity;
  238.                     parity <= 0;
  239.  
  240.                     state <= 4;
  241.                 end else begin
  242.                     counter <= counter - 1'b1;
  243.                     ps2_data_out <= cmd [0];
  244.                     parity <= parity ^ (cmd [0]);
  245.                     cmd <= cmd >> 1;
  246.                 end
  247.                 4: begin
  248.                     // stop bit
  249.                     ps2_data_out <= 0;
  250.                     ps2_data_enable <= 0;
  251.  
  252.                     state <= 5;
  253.                 end
  254.                 5: if (!ps2_data_in) begin
  255.                     busy <= 0;
  256.  
  257.                     state <= 0;
  258.                 end
  259.  
  260.                 // receive states
  261.                 6: if (counter == 0) begin
  262.                     // parity
  263.                     // TODO: parity check
  264.                     parity <= 0;
  265.  
  266.                     state <= 7;
  267.                 end else begin
  268.                     cmd_in <= (cmd_in >> 1) | {ps2_data_in, 7'b0000000};
  269.                     counter <= counter - 1'b1;
  270.                 end
  271.                 7: begin
  272.                     // stop bit
  273.                     busy <= 0;
  274.                     state <= 8;
  275.                 end
  276.             endcase
  277.         end
  278.     end else begin
  279.         cycle <= 1;
  280.     end
  281. end
  282.  
  283. endmodule
  284.  
  285. module sync (
  286.     input clk,
  287.  
  288.     input in,
  289.     output reg out
  290. );
  291.  
  292. reg r0 = 0;
  293.  
  294. always @(posedge clk) begin
  295.     r0 <= in;
  296.     out <= r0;
  297. end
  298.  
  299. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement