Advertisement
wgma

Untitled

Jul 24th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module shifter_grid(startGameEn, shoot, clock, gridUpdateEn, user_x, enemy_x, grid);
  2.     input startGameEn; // reset the grid from SW[2]
  3.     input shoot; // shoot input from SW[1]
  4.     input clock; // default 50mhz clock input
  5.      input gridUpdateEn;
  6.     input [7:0]user_x; // player's position on the x plane
  7.      input [7:0]enemy_x; // enemy's position on the x plane
  8.  
  9.     output [160*120:0]grid; // 2d grid we're doing logic on, interperet it as paritions of 120
  10.      
  11.      
  12.      // lets try slowing down the shifter bit
  13.      wire [27:0]rd_1hz_out;
  14.      rate_divider rd_1hz(
  15.         .enable(1'b1),
  16.         .countdown_start(28'd49_999_999), // 49,999,99 in dec
  17.         .clock(clock),
  18.         .reset(startGameEn),
  19.         .q(rd_1hz_out)
  20.      );
  21.      
  22.      wire shifter_bit_clock = rd_1hz_out == 28'b0 ? 1:0;
  23.      
  24.      // generate 160 shifter bit lines, each consisting of
  25.      // 120 shifter bits.
  26.      genvar i;
  27.      generate
  28.  
  29.          for(i = 0;i < 160;i = i+1) begin: shifter_grids
  30.                 shifter shift_i(
  31.                     // only load_val value we care about is at the first shifter bit
  32.                     .load_val(user_x == i ? shoot : 1'b0),
  33.                     .load_n(shoot),
  34.                     .shift_right(1'b1),
  35.                     .ASR(1'b0),
  36.                     .clk(shifter_bit_clock),
  37.                     .reset_n(startGameEn),
  38.                     // interpret partitions like 0..120, 121...140, and
  39.                     // have the shifterbit logic be outputed on these parts
  40.                     // of the 2d grid
  41.                     .Q(grid[120*(i+1)-1: 120*i])
  42.                 );
  43.          end
  44.      
  45.      endgenerate
  46.  
  47. endmodule
  48.  
  49. module mux2to1(x, y, s, m);
  50.     input x; // first value to choose from
  51.     input y; // second value to choose from
  52.     input s; // signal uses to determine which value to output
  53.     output m; // where to store selection
  54.  
  55.    // s = 1'b0 => x
  56.     // s = 1'b1 => y
  57.     assign m = s & y | ~s & x;
  58.  
  59. endmodule
  60.  
  61. module flipflop(d, q, clock, reset_n);
  62.    // note: that this is a 1 bit register
  63.     input d;
  64.     input clock, reset_n;
  65.     output reg q;
  66.    
  67.     always @(posedge clock) // Triggered every time clock rises
  68.     begin
  69.         if(reset_n == 1'b1) // When reset_n is 0 (note this is tested on every rising clock edge)
  70.             q <= 0;               // q is set to 0. Note that the assignment uses <= since this isn't a combintorial circuit
  71.         else                      // when reset_n is not 0
  72.             q <= d;           // value of d passes through to output q
  73.     end
  74. endmodule
  75.  
  76. module shifter_bit(in, load_val, shift, load_n, ignore_load_n, clk, reset_n, out);
  77.   // Note that these should all be 1 bit inputs as we're really only handling/storing one bit of information in shifter bit
  78.   input in; // connected to out port of left shifter, 0 otherwise on left most shifter bit
  79.   input load_val; // input given from switches, used onlywhen shift = 0
  80.   input shift;  // indicates to shift all bits right
  81.   input load_n; // indicates to load input from switches
  82.   input ignore_load_n; // ignore signal from load_n
  83.   input clk;  // clock used for flip flop
  84.   input reset_n;  // reset signal to set shifter bit's value to 0
  85.   output out; // output of value in shifter bit, generally sent to shifter bit on right
  86.  
  87.   wire mux_one_out, mux_two_out;
  88.  
  89.   // determine's whether to shift the bit or not
  90.   mux2to1 mux_one(
  91.         .x(out),
  92.         .y(in),
  93.         .s(shift),
  94.         .m(mux_one_out)
  95.   );
  96.   // determine's whether to load the value from load_val or from in(from left shifter_bit)
  97.   mux2to1 mux_two(
  98.         .x(load_val),
  99.         .y(mux_one_out),
  100.         .s(ignore_load_n | load_n), // if ignore_load_n is high we load values from mux_one_out
  101.         .m(mux_two_out)
  102.   );
  103.   // determine's logic for what bit should be sent to next shifter_bit module
  104.   flipflop flip_flop(
  105.         .d(mux_two_out),
  106.         .q(out),
  107.         .clock(clk),
  108.         .reset_n(reset_n)
  109.   );
  110. endmodule
  111.  
  112. module shifter(load_val, load_n, shift_right, ASR, clk, reset_n, Q);
  113.   input load_val; // loadval for row 0
  114.   input load_n; // global load_n value for all shifter bits, indicates whether to load values from load_val into each shifter bit
  115.   input shift_right; // global shift value for all shifter bits, indicates to shift bits value to next shifter_bit
  116.   input ASR; // determine if we are to perform sign extension; i.e. 101 (-1 signed) -> 110 (-2 signed), instead of 101 (6 unsigned) -> 010 (2 unsigned)
  117.   input clk; // global clock to use for all of our flip flops
  118.   input reset_n; // global reset_n value for all our flip fops in shifter_bits, which sets their output/value to 0
  119.  
  120.   output [120:0]Q; // output register (generally LEDR[7:0]) we're to show value of shifter on
  121.  
  122.   wire [120:0]sb_out;
  123.  
  124.   genvar i;
  125.   generate
  126.  
  127.       for(i = 120;i >= 1;i = i-1) begin: shifter_bit_init
  128.               shifter_bit sb_i(
  129.                   .in( (i == 120 ? ASR & load_val : sb_out[i]) ),
  130.                   .load_val(load_val),
  131.                   .shift(shift_right),
  132.                   .load_n(load_n),
  133.                   .ignore_load_n(i == 120 ? 1: 0),
  134.                   .clk(clk),
  135.                   .reset_n(reset_n),
  136.                   .out(sb_out[i-1])
  137.               );
  138.         end
  139.  
  140.  
  141.   endgenerate
  142.  
  143.   assign Q = sb_out[120:0];
  144.  
  145.  
  146. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement