Advertisement
RickCHodgin

Non-functional sram led code

May 6th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////
  2. // Top menu
  3. //////
  4.     module top(clk, sw19, sw50, sw52, sw53, sw54, leds, write_address, write_data, write_enable, read_address, read_data, read_enable);
  5.  
  6.         //////////
  7.         // Parameters
  8.         //////
  9.             // Input
  10.             input wire            clk;
  11.             input wire            sw19;
  12.             input wire            sw50;
  13.             input wire            sw52;
  14.             input wire            sw53;
  15.             input wire            sw54;
  16.            
  17.             // Output
  18.             output wire [7:0]    leds;
  19.             output wire [7:0]    write_address;
  20.             output wire [7:0]    write_data;
  21.             output wire          write_enable;
  22.             output wire [7:0]    read_address;
  23.             output wire [7:0]    read_data;
  24.             output wire          read_enable;
  25.            
  26.        
  27.         //////////
  28.         // Local variables
  29.         //////
  30.             wire                 writing;
  31.             reg                  mode;
  32.            
  33.             initial
  34.             begin
  35.                 mode             = 1'b0;
  36.             end
  37.        
  38.        
  39.         //////////
  40.         // Clock
  41.         //////
  42.             wire                 clk1;    // 10 MHz
  43.             wire                 clkOk;
  44.             my_pll    pll100 (.CLK(clk), .CLKOP(clk1), .LOCK(clkOk));
  45.        
  46.        
  47.         //////////
  48.         // Sub-clocks
  49.         //////
  50.             wire [31:0]          subclk1000_count;
  51.             wire                 subclk1000;
  52.             // 10 KHz clock
  53.             subclock #(1000) clk_1000(clk1, subclk1000_count[31:0], subclk1000);
  54.            
  55.             wire [31:0]          subclk100_count;
  56.             wire                 subclk100;
  57.             // 100 KHz clock
  58.             subclock #(100)  clk_100(clk1, subclk100_count[31:0], subclk100);
  59.        
  60.        
  61.         //////////
  62.         // Main loop
  63.         //////
  64.             // Reads buttons presses, displays the memory at that location
  65.             led_switch         main  (subclk1000, sw19, sw50, sw52, sw53, sw54, leds, mode, read_address, read_data, read_enable, writing);
  66.            
  67.             // Used only at startup, where it writes data to the first 256 bytes of memory (00h..FFh)
  68.             read_write_memory  rwm   (mode, subclk1000, subclk100, write_address, write_data, write_enable, read_address, read_data, read_enable, writing);
  69.  
  70.     endmodule
  71.  
  72.  
  73. //////////
  74. // LED switches
  75. //////
  76.     module led_switch(clk_1000, sw19, sw50, sw52, sw53, sw54, leds, mode, read_address, read_data, read_enable, writing);
  77.        
  78.         //////////
  79.         // Parameters
  80.         //////
  81.             // Input
  82.             input  wire         clk_1000;
  83.             input  wire         sw19;
  84.             input  wire         sw50;
  85.             input  wire         sw52;
  86.             input  wire         sw53;
  87.             input  wire         sw54;
  88.             input  wire         mode;
  89.             input  wire[7:0]    read_address;
  90.             input  wire[7:0]    read_data;
  91.             input  wire         read_enable;
  92.             input  wire         writing;
  93.            
  94.             // Output
  95.             output reg [7:0]    leds;
  96.  
  97.        
  98.         //////////
  99.         // Switches up/down the address
  100.         //////
  101.             always @(posedge clk_1000)
  102.             begin
  103.                 // Switch 4
  104.                 if (sw53 == 1'b0)
  105.                 begin
  106.                     // Decrease address
  107.                     // if address > 0x00
  108.                 end
  109.                
  110.                 // Switch 5
  111.                 if (sw54 == 1'b0)
  112.                 begin
  113.                     // Increase address
  114.                     // if address < 0xff
  115.                 end
  116.                
  117.                 // Display the value
  118.                 leds[7:0] <= read_data[7:0];
  119.             end
  120.  
  121.     endmodule
  122.  
  123.  
  124. //////////
  125. // Sub-clock definition
  126. //////
  127.     module subclock(clk_in, count, clk_out);
  128.        
  129.         parameter            MAX = 10;
  130.         input  wire          clk_in;
  131.         output reg[31:0]     count;
  132.         output reg           clk_out;
  133.        
  134.         // Initialize count to 0, and clock low
  135.         initial
  136.         begin
  137.             count    = 32'b0;
  138.             clk_out  = 1'b0;
  139.         end
  140.  
  141.         // Based on input clock, cycle through
  142.         always @(posedge clk_in)
  143.         begin
  144.             // Increase the count
  145.             count <= count + 32'b1;
  146.            
  147.             // Once we reach our max, we're there
  148.             if (count > MAX - 1)
  149.             begin
  150.                 // Reset the count
  151.                 count <= 32'b0;
  152.                
  153.                 // Flipi the clock out
  154.                 clk_out = ~clk_out;
  155.             end
  156.         end
  157.        
  158.     endmodule
  159.  
  160.  
  161. //////////
  162. // Read/write memory
  163. //////
  164.     module read_write_memory (mode, clk_slow, clk_fast, write_address, write_data, write_enable, read_address, read_data, read_enable, writing);
  165.        
  166.         //////////
  167.         // Input/output
  168.         //////
  169.             input  wire         mode;
  170.             input  wire         clk_slow;         // .001 main clock
  171.             input  wire         clk_fast;         // .01 main clock
  172.            
  173.             input  wire[7:0]    read_address;     // Read info
  174.             input  wire[7:0]    read_data;
  175.             input  wire         read_enable;
  176.            
  177.             output wire[7:0]    write_address;    // Write info
  178.             output wire[7:0]    write_data;
  179.             output wire         write_enable;
  180.             output reg          writing;          // Flag indicating we're still writing during the initial bootup
  181.        
  182.  
  183.  
  184.         //////////
  185.         // Memory
  186.         //////
  187.             reg [7:0]           waddress;         // Write address
  188.             reg [7:0]           wdata;            // Write data
  189.             reg [7:0]           raddress;         // Read address
  190.            
  191.             reg                 wenable;
  192.             reg                 renable;
  193.  
  194.             // Initialized values
  195.             initial begin
  196.                 waddress        = 8'b_0000_0000;
  197.                 data            = 8'b_1111_1111;
  198.                
  199.                 raddress        = 8'b_1000_0000;
  200.            
  201.                 wenable         = 1'b0;
  202.                 renable         = 1'b0;
  203.                 writing         = 1'b1;
  204.             end
  205.            
  206.  
  207.         //////////
  208.         // Pass-thru
  209.         //////
  210.             assign write_address = ((writing) ? waddress[7:0] : 8'bZZZZZZZZ);
  211.             assign write_data    = ((writing) ? data[7:0]     : 8'bZZZZZZZZ);
  212.             assign write_enable  = ((writing) ? wenable       : 1'bZ);
  213.        
  214.        
  215.         //////////
  216.         // Main
  217.         //////
  218.             // Turn off strobes
  219.             always @(posedge clk_fast)
  220.             begin
  221.                 if (wenable)    wenable <= 1'b0;        // Write enable
  222.                 if (renable)    renable <= 1'b0;        // Read enable
  223.             end
  224.        
  225.             // Handle address and data
  226.             always @(posedge clk_slow)
  227.             begin
  228.                 if (writing == 1'b0)
  229.                 begin
  230.                     // Reading
  231.                     // ?? don't know what to do
  232.                 end
  233.                 else
  234.                 begin
  235.                     // Writing
  236.                     waddress[7:0]  <= waddress[7:0]  + 8'b1;        // Next address
  237.                     data[7:0]      <= data[7:0]      - 8'b1;        // New data value
  238.                    
  239.                     // Raise the write flags
  240.                     wenable        <= 1'b1;
  241.                     writing        <= ((waddress[7:0] == 8'b_1111_1111) ? 1'b0 : 1'b1);
  242.                 end
  243.             end
  244.  
  245.     endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement