Advertisement
Guest User

gcController Review

a guest
May 4th, 2018
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //##################################################################################################
  2. //  module: gcController.sv
  3. //  desc:   driver for one wire interface used by gamecube controllers
  4. //  params: p_clkFreq         : input clock frequency, default is 100 MHz
  5. //          p_controllerClock : controller interface clock frequency, default is 250 KHz
  6. //          p_updateRate      : controller data refresh rate, default is 100
  7. //  io:     clk               : clock
  8. //          rst_n             : asynchronus active low reset
  9. //          data              : controller bidirectional data pin
  10. //          jStickX           : analog joystick 8-bit x value
  11. //          jStickY           : analog joystick 8 bit y value
  12. //          cStickX           : analog cstick 8 bit x
  13. //          faceButtons       : current face button values, a/b/x/y/start
  14. //          dPad              : current d pad values
  15. //          shoulderButtons   : current shoulder button values, l/r/z
  16. //          newDataAvaliable  : new controller data is ready
  17. //          controllerPresent : controller is present, active high
  18. //##################################################################################################
  19. module gcController
  20. (
  21.   input wire clk,
  22.   input wire rst_n,
  23.   inout wire data,
  24.   output wire [4:0] faceButtons,
  25.   output wire [3:0] dPad,
  26.   output wire [2:0] shoulderButtons,
  27.   output wire [7:0] jStickX,
  28.   output wire [7:0] jStickY,
  29.   output wire [7:0] cStickX,
  30.   output wire [7:0] cStickY,
  31.   output wire [7:0] lAnalog,
  32.   output wire [7:0] rAnalog,
  33.   output wire newDataAvaliable,
  34.   output wire controllerPresent
  35. );
  36.  
  37.   `default_nettype none
  38.  
  39.   // input clock frequency, controller update rate, controller interface frequency
  40.   parameter p_clkFreq = 100000000;
  41.   parameter p_updateRate = 100;
  42.   parameter p_controllerClock = 250000;
  43.  
  44.   // number of clock ticks per sub bit pattern, number of clock ticks per update period, and
  45.   // number of clock ticks for read timeout
  46.   localparam c_subBitClkTicks = (p_clkFreq / p_controllerClock) / 4;
  47.   localparam c_timeoutClkTicks = (p_clkFreq / p_controllerClock);
  48.   localparam c_updateRateClkTicks = (p_clkFreq / p_updateRate);
  49.  
  50.   // data and probe packets including stop bits
  51.   localparam c_probeSequence = 9'b100000000;
  52.   localparam c_dataRequestSequence = 25'b1010000001100000000000010;
  53.  
  54.   // reponse packet (stadard oem controller) to probe sequence, needs work to determine what bits
  55.   // mean what from different controller types
  56.   localparam c_probeResponseSequence = 24'b000010010000000000100000;
  57.   localparam c_stopBit = 1'b1;
  58.  
  59.   // controller data
  60.   typedef struct packed
  61.   {
  62.     reg [2:0] zeros;
  63.     reg [4:0] faceButtons;
  64.     reg one;
  65.     reg [3:0] dPad;
  66.     reg [2:0] shoulderButtons;
  67.     reg [7:0] jStickX;
  68.     reg [7:0] jStickY;
  69.     reg [7:0] cStickX;
  70.     reg [7:0] cStickY;
  71.     reg [7:0] lAnalog;
  72.     reg [7:0] rAnalog;
  73.     reg stop;
  74.   } sGcData;
  75.  
  76.   typedef union packed
  77.   {
  78.     sGcData bits;
  79.     reg [64:0] buffer;
  80.   } uGcData;
  81.  
  82.   // one hot state indices
  83.   enum
  84.   {
  85.     IDLE                      = 0,
  86.     SEND_CONTROLLER_PACKET    = 1,
  87.     TIMEOUT                   = 2,
  88.     GET_CONTROLLER_RESPONSE   = 3
  89.   } gcStates;
  90.  
  91.   // open drain states
  92.   enum
  93.   {
  94.     WRITE = 0,
  95.     READ = 1
  96.   } openDrainStates;
  97.  
  98.   // current state of bus protocol
  99.   enum
  100.   {
  101.     PROBE_REQ_SENT = 0,
  102.     DATA_REQ_SENT = 1
  103.   } masterBusAction;
  104.  
  105.   // counters
  106.   reg [($clog2(c_updateRateClkTicks) - 1):0] updateCount;
  107.   reg [($clog2(c_subBitClkTicks) - 1):0] subBitCount;
  108.   reg [($clog2(c_timeoutClkTicks) - 1):0] timeoutCount;
  109.   reg [2:0] subPatternCount;
  110.   reg [6:0] bitCount;
  111.  
  112.   // counter resets
  113.   logic resetTimeoutCount;
  114.   logic resetUpdateCount;
  115.   logic resetSubBitCount;
  116.  
  117.   // counter gating/done signals
  118.   logic updateCountDone;
  119.   logic subBitCountDone;
  120.   logic subPatternDone;
  121.   logic timeoutCountDone;
  122.   logic subBitCountEnable;
  123.  
  124.   // state vectors
  125.   reg [3:0] state, nState;
  126.  
  127.   // control signals
  128.   logic controllerIsThere;
  129.   logic haveValidData;
  130.   logic readWrite;
  131.   logic halfBitWait;
  132.   logic shiftDone;
  133.   logic readDone;
  134.   logic sentProbe;
  135.   logic sentData;
  136.   logic probeOrData;
  137.  
  138.   // sub pattern shift register control signals
  139.   logic newSubBit;
  140.   logic subBitShiftDone;
  141.  
  142.   // data shift register control signals
  143.   logic dataShiftDone;
  144.   logic finishShift;
  145.  
  146.   // data and probe packets, including stop bits
  147.   reg [8:0] probeSequence;
  148.   reg [24:0] dataRequestSequence;
  149.  
  150.   // data
  151.   uGcData gcData;
  152.   reg [3:0] subPattern;
  153.  
  154.   // startup values
  155.   initial begin
  156.     gcData.buffer = 64'd0;
  157.     probeSequence = c_probeSequence;
  158.     dataRequestSequence = c_dataRequestSequence;
  159.   end
  160.  
  161.   // next state assigment
  162.   always_ff @ (posedge clk) begin
  163.     if (!rst_n) begin
  164.       state[3:1] <= 'd0;
  165.       state[IDLE] <= 1'b1;
  166.     end
  167.     else
  168.       state <= nState;
  169.   end
  170.  
  171.   // next state and output logic
  172.   always_comb begin
  173.  
  174.     nState = 5'b0;
  175.  
  176.     readWrite = READ;
  177.     haveValidData = 1'b0;
  178.  
  179.     sentProbe = 0;
  180.     sentData = 0;
  181.  
  182.     subBitCountEnable = 1'b0;
  183.     resetUpdateCount = 1'b0;
  184.     resetSubBitCount = 1'b0;
  185.     resetTimeoutCount = 1'b0;
  186.  
  187.     unique case (1'b1)
  188.  
  189.       state[IDLE]: begin
  190.  
  191.         // open drain read, off the bus
  192.         readWrite = READ;
  193.  
  194.         // enable update rate counter, send probe/data when it expires
  195.         if (updateCountDone) begin
  196.           resetUpdateCount = 1'b1;
  197.           nState[SEND_CONTROLLER_PACKET] = 1'b1;
  198.         end
  199.         else begin
  200.           nState[IDLE] = 1'b1;
  201.           resetUpdateCount = 1'b0;
  202.         end
  203.  
  204.       end
  205.  
  206.       state[SEND_CONTROLLER_PACKET]: begin
  207.  
  208.         // open drain write, on the bus
  209.         readWrite = WRITE;
  210.  
  211.         // latch current protocol state for read/check logic
  212.         sentProbe = controllerIsThere ? 0 : 1;
  213.         sentData = controllerIsThere ? 1 : 0;
  214.  
  215.         // enable write logic and wait for controller response
  216.         if (subBitCountDone) begin
  217.           resetSubBitCount = 1'b1;
  218.           subBitCountEnable = 1'b0;
  219.           nState[TIMEOUT] = 1'b1;
  220.           resetTimeoutCount = 1'b1;
  221.         end
  222.         else begin
  223.           subBitCountEnable = 1'b1;
  224.           resetSubBitCount = 1'b0;
  225.           resetTimeoutCount = 1'b0;
  226.           nState[SEND_CONTROLLER_PACKET] = 1'b1;
  227.         end
  228.  
  229.       end
  230.  
  231.       state[TIMEOUT]: begin
  232.  
  233.         // open drain read, off the bus
  234.         readWrite = READ;
  235.  
  236.         // enable timeout counter, if slave pulls bus low read response else go back to idle
  237.         if (timeoutCountDone)
  238.           nState[IDLE] = 1'b1;
  239.         else if (!data)
  240.           nState[GET_CONTROLLER_RESPONSE] = 1'b1;
  241.         else
  242.           nState[TIMEOUT] = 1'b1;
  243.          
  244.       end
  245.  
  246.       state[GET_CONTROLLER_RESPONSE]: begin
  247.  
  248.         // open drain read, off the bus
  249.         readWrite = READ;
  250.  
  251.         // enable read logic and wait for done
  252.         if (readDone) begin
  253.           resetSubBitCount = 1;
  254.           subBitCountEnable = 0;
  255.           haveValidData = (probeOrData == DATA_REQ_SENT) ? 1 : 0;
  256.           nState[IDLE] = 1'b1;
  257.         end
  258.         else begin
  259.           subBitCountEnable = 1;
  260.           resetSubBitCount = 0;
  261.           haveValidData = 0;  
  262.           nState[GET_CONTROLLER_RESPONSE] = 1;
  263.         end
  264.  
  265.       end
  266.  
  267.     endcase
  268.  
  269.   end
  270.  
  271.   // previous master bus action
  272.   always_ff @ (posedge clk) begin
  273.     if (!rst_n)
  274.       probeOrData <= PROBE_REQ_SENT;
  275.     else if (sentProbe && !sentData)
  276.       probeOrData <= PROBE_REQ_SENT;
  277.     else if (sentData && !sentProbe)
  278.       probeOrData <= DATA_REQ_SENT;
  279.     else
  280.       probeOrData <= probeOrData;
  281.   end
  282.  
  283.   // counter for update rate
  284.   always_ff @ (posedge clk) begin
  285.  
  286.     if (!rst_n || resetUpdateCount) begin
  287.       updateCount <= {$clog2(c_updateRateClkTicks){1'b0}};
  288.       updateCountDone <= 1'b0;
  289.     end
  290.     else if (updateCount == c_updateRateClkTicks - 1) begin
  291.       updateCount <= {$clog2(c_updateRateClkTicks){1'b0}};
  292.       updateCountDone <= 1'b1;
  293.     end
  294.     else begin
  295.       updateCount <= updateCount + 1;
  296.       updateCountDone <= 1'b0;
  297.     end
  298.  
  299.   end
  300.  
  301.   // counter for read timeout
  302.   always_ff @ (posedge clk) begin
  303.  
  304.     if (!rst_n || resetTimeoutCount) begin
  305.       timeoutCount <= 0;
  306.       timeoutCountDone <= 0;
  307.     end
  308.     else if (timeoutCount == (c_timeoutClkTicks - 1)) begin
  309.       timeoutCount <= 0;
  310.       timeoutCountDone <= 1;
  311.     end
  312.     else begin
  313.       timeoutCount <= timeoutCount + 1;
  314.       timeoutCountDone <= 0;
  315.     end
  316.    
  317.   end
  318.  
  319. // counter for sub-bits and bits
  320.   always_ff @ (posedge clk) begin
  321.  
  322.     halfBitWait <= halfBitWait;
  323.     bitCount <= bitCount;
  324.     subBitCountDone <= 0;
  325.     subPatternDone <= 0;
  326.     newSubBit <= 0;
  327.  
  328.     if (!rst_n || resetSubBitCount) begin
  329.       halfBitWait <= 0;
  330.       subPatternDone <= 0;
  331.       subBitCount <= 'd0;
  332.       subPatternCount <= 'd0;
  333.       bitCount <= 'd0;
  334.       newSubBit <= 0;
  335.     end
  336.     else if (readWrite == READ && !halfBitWait && subBitCount == ((c_subBitClkTicks / 2) - 1)) begin
  337.       subBitCount <= 0;
  338.       subPatternDone <= 0;
  339.       bitCount <= bitCount;
  340.       halfBitWait <= 1;
  341.       subPatternCount <= subPatternCount + 1;
  342.       newSubBit <= 1;
  343.     end
  344.     else if (subBitCount == c_subBitClkTicks - 1) begin
  345.       newSubBit <= 1;
  346.       subBitCount <= 'd0;
  347.       if (readWrite == WRITE && (bitCount == (controllerIsThere ? 'd24 : 'd8)) && subPatternCount == 2) begin
  348.         subBitCountDone <= 1;
  349.         bitCount <= 'd0;
  350.       end
  351.       else if (readWrite == READ && bitCount == (controllerIsThere ? 'd64 : 'd24) && subPatternCount == 3) begin
  352.         subBitCountDone <= 1;
  353.         bitCount <= 'd0;
  354.         subPatternDone <= 1;
  355.       end
  356.       else if (subPatternCount == 3) begin
  357.         subPatternCount <= 'd0;
  358.         subPatternDone <= 1;
  359.         bitCount <= bitCount + 1;
  360.       end
  361.       else begin
  362.         subPatternCount <= subPatternCount + 1;
  363.         subPatternDone <= 0;
  364.       end
  365.     end
  366.     else begin
  367.       subBitCount <= (subBitCountEnable) ? (subBitCount + 1) : subBitCount;
  368.       subPatternCount <= subPatternCount;
  369.       bitCount <= bitCount;
  370.       subPatternDone <= 0;
  371.       newSubBit <= 0;
  372.     end
  373.    
  374.   end
  375.  
  376.   // latch sub-bit count done for data shift done
  377.   always_ff @ (posedge clk) begin
  378.    
  379.     if (!rst_n || resetSubBitCount)
  380.       finishShift <= 0;
  381.     else if (subBitCountDone)
  382.       finishShift <= 1;
  383.     else
  384.       finishShift <= finishShift;
  385.  
  386.   end
  387.  
  388.   // 4 bit sub pattern shift register
  389.   always_ff @ (posedge clk) begin
  390.  
  391.     subBitShiftDone <= 0;
  392.  
  393.     if (!rst_n)
  394.       subPattern <= 'd0;
  395.     else if (readWrite == READ && newSubBit) begin
  396.       subPattern <= {subPattern[2:0], data};
  397.       if (subPatternDone)
  398.         subBitShiftDone <= 1;
  399.       else
  400.         subBitShiftDone <= 0;
  401.     end
  402.     else
  403.       subPattern <= subPattern;
  404.  
  405.   end
  406.  
  407.   // data buffer shift register
  408.   always_ff @ (posedge clk) begin
  409.  
  410.     shiftDone <= 0;
  411.  
  412.     if (!rst_n)
  413.       gcData.buffer = 'd0;
  414.     else if (readWrite == READ && subBitShiftDone) begin
  415.       gcData.buffer <= {gcData.buffer[63:0], (~subPattern[3] & (&subPattern[2:0]))};
  416.       if (finishShift)
  417.         shiftDone <= 1;
  418.       else
  419.         shiftDone <= 0;
  420.     end
  421.     else
  422.       gcData.buffer <= gcData.buffer;
  423.   end
  424.  
  425.   // data check logic
  426.   always_ff @ (posedge clk) begin
  427.  
  428.     if (!rst_n) begin
  429.       readDone <= 0;
  430.       controllerIsThere <= 0;
  431.     end
  432.     else if (shiftDone && readWrite == READ) begin
  433.       readDone <= 1;
  434.       if (probeOrData == PROBE_REQ_SENT) begin
  435.         if (gcData.buffer[24:1] == c_probeResponseSequence /*&& gcData.buffer[0] == c_stopBit*/)
  436.           controllerIsThere <= 1;
  437.         else
  438.           controllerIsThere <= 0;
  439.       end
  440.       if (probeOrData == DATA_REQ_SENT) begin
  441.         if (gcData.bits.zeros != 'd0 && gcData.bits.one != 'd1)
  442.           controllerIsThere <= 0;
  443.         else
  444.           controllerIsThere <= 1;
  445.       end
  446.     end
  447.     else begin
  448.       readDone <= 0;
  449.       controllerIsThere <= controllerIsThere;
  450.     end
  451.  
  452.   end
  453.  
  454.   // data input/output logic
  455.   assign data = (readWrite == READ) ? 1'bZ :
  456.                 (probeOrData == PROBE_REQ_SENT) ?
  457.                 (probeSequence[bitCount[3:0]] ?
  458.                 ((subPatternCount < 1 && subBitCountEnable) ? 1'b0 : 1'bZ) :
  459.                 ((subPatternCount < 3 && subBitCountEnable) ? 1'b0 : 1'bZ)) :
  460.                 (probeOrData == DATA_REQ_SENT) ?
  461.                 (dataRequestSequence[bitCount[4:0]] ?
  462.                 ((subPatternCount < 1 && subBitCountEnable) ? 1'b0 : 1'bZ) :
  463.                 ((subPatternCount < 3 && subBitCountEnable) ? 1'b0 : 1'bZ)) :
  464.                 1'bZ;
  465.  
  466.   // controller presence
  467.   assign controllerPresent = controllerIsThere ? 1'b1 : 1'b0;
  468.  
  469.   // controller data ready
  470.   assign newDataAvaliable = haveValidData ? 1'b1 : 1'b0;
  471.  
  472.   // controller data fields
  473.   assign faceButtons      = gcData.bits.faceButtons;
  474.   assign dPad             = gcData.bits.dPad;
  475.   assign shoulderButtons  = gcData.bits.shoulderButtons;
  476.   assign jStickX          = gcData.bits.jStickX;
  477.   assign jStickY          = gcData.bits.jStickY;
  478.   assign cStickX          = gcData.bits.cStickX;
  479.   assign cStickY          = gcData.bits.cStickY;
  480.   assign lAnalog          = gcData.bits.lAnalog;
  481.   assign rAnalog          = gcData.bits.rAnalog;
  482.  
  483. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement