Advertisement
RickCHodgin

buggy sram code

Apr 29th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module top (c, l, a, d, r, w, up, dn);
  2.     input    wire          c;     // clock
  3.     output   wire [7:0]    l;     // leds
  4.     output   wire[16:0]    a;     // address
  5.     inout    wire [7:0]    d;     // data
  6.     output   wire          r;     // read
  7.     output   wire          w;     // write
  8.     input    wire          up;    // up button
  9.     input    wire          dn;    // down button
  10.  
  11.     // Clock
  12.     wire    clk1;
  13.     wire    clk2;
  14.     wire    clkOk;
  15.     pll pll0 (.CLK(c), .CLKOP(clk1), .CLKOK(clk2), .LOCK(clkOk));
  16.  
  17.     // Main module
  18.     sram_read_write sramscope (.clock(c), .address(a), .data(d), .read(r), .write(w), .leds(l), .up(up), .down(dn));
  19. endmodule
  20.  
  21. module sram_read_write (clock, address, data, read, write, leds, up, down);
  22.     // I/O
  23.     input    wire        clock;
  24.     output   reg[16:0]   address;
  25.     inout        [7:0]   data;
  26.     output   reg [7:0]   leds;
  27.     output   reg         write;
  28.     output   reg         read;
  29.     input    wire        up;
  30.     input    wire        down;
  31.  
  32.     // Local variables
  33.     reg     [3:0]        counter;
  34.     reg    [16:0]        laddress;
  35.     reg     [7:0]        ldata;
  36.     reg                  operation;
  37.    
  38.     // Setup
  39.     initial
  40.     begin
  41.         counter[3:0]    = 4'b0;
  42.         laddress[16:0]  = 17'b1_1111_1111_1111_1111;
  43.         ldata[7:0]      = 8'b1111_1111;
  44.         operation       = 0;
  45.     end
  46.  
  47.     // Assignments
  48.     assign data = write ? ldata : 8'b0;
  49.  
  50.     // Main loop
  51.     always @(posedge clock)
  52.     begin
  53.         // Assign the leds so the value stored there shows up
  54.         leds[7:0] = ldata[7:0];
  55.        
  56.  
  57.         // Have we gotten to 1 yet?
  58.         counter <= counter + 4'b1;
  59.         if (counter > 4'b1)
  60.         begin
  61.             // Turn off any read prior read or write as they only strobe two clocks
  62.             write <= 1'b0;
  63.             read  <= 1'b0;
  64.         end
  65.    
  66.    
  67.         // Have we gotten to 10 yet?
  68.         if (counter > 9)
  69.         begin
  70.             // Ready to perform the next operation
  71.             // Note:  For the first 255 operations, we write from 0..255 in SRAM
  72.             // Note:  After that, we respond to up/dn presses to display the next or previous address
  73.            
  74.             // Reset the counter
  75.             counter <= 1'b0;
  76.            
  77.             // Are we writing or reading?
  78.             if (operation == 1'b0)
  79.             begin
  80.                 // We're writing to the SRAM
  81.            
  82.                 // Increase our address
  83.                 laddress[16:0] <= laddress[16:0] + 17'b1;
  84.                 address[16:0]  <= laddress[16:0];
  85.                
  86.                 // Shift the data value
  87.                 ldata <= (ldata - 1'b1);
  88.                
  89.                 // Strobe write
  90.                 write <= 1'b1;
  91.                
  92.                 // If we're done, switch operation
  93.                 if (ldata[7:0] == 7'b0)
  94.                 begin
  95.                     operation <= 1'b1;
  96.                 end
  97.                
  98.             end else begin
  99.                 // We're displaying what's in the SRAM
  100.                
  101.                 // Are we going up?
  102.                 if (up == 1'b1)
  103.                 begin
  104.                     // They're going up in address
  105.                     laddress <= laddress + 1'b1;
  106.                 end
  107.                
  108.                 // Are we going down?
  109.                 if (down == 1'b1)
  110.                 begin
  111.                     // They're going down in address
  112.                     laddress <= laddress - 1'b1;
  113.                 end
  114.  
  115.                 // Strobe read
  116.                 read <= 1'b1;
  117.             end
  118.         end
  119.     end
  120. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement