title: How can I use iCE40 4K block RAM in 512x8 read mode with IceStorm?
body: <p>I am trying to figure out how to use the block RAM on my iCE40HX-8K Breakout Board. I would like to access it in a 512x8 configuration, which as far as I can tell from the documentation is supported by project IceStorm, but I haven't been able to get it to work like I expected.</p>
  
  <p>If I understand correctly, initializing an SB_RAM40_4K primitive with the READ_MODE parameter set to 1 should set the block up in 512x8 read mode, which uses a 9 bit read address, and reads 8 bits of data at each address.</p>
  
  <p>Here is the simplest example I could think of. It sets up an SB_RAM40_4K with some pre-initialized memory and reads straight to the pins of the on-board LED's.</p>
  
  <p>hx8kboard.pcf</p>
  
  <pre><code>set_io leds[0] B5
  set_io leds[1] B4
  set_io leds[2] A2
  set_io leds[3] A1
  set_io leds[4] C5
  set_io leds[5] C4
  set_io leds[6] B3
  set_io leds[7] C3
  set_io clk J3
  </code></pre>
  
  <p>top.v</p>
  
  <pre><code>module top (
    output [7:0] leds,
    input clk
  );
  
  //reg [8:0] raddr = 8'd0;
  reg [8:0] raddr = 8'd1;
  
  SB_RAM40_4K #(
    .INIT_0(256'h00000000000000000000000000000000000000000000000000000000_44_33_22_11),
    .WRITE_MODE(1),
    .READ_MODE(1)
  ) ram40_4k_512x8 (
    .RDATA(leds),
    .RADDR(raddr),
    .RCLK(clk),
    .RCLKE(1'b1),
    .RE(1'b1),
    .WADDR(8'b0),
    .WCLK(1'b0),
    .WCLKE(1'b0),
    .WDATA(8'b0),
    .WE(1'b0)
  );
  
  endmodule
  </code></pre>
  
  <p>LED output when raddr == 0</p>
  
  <pre><code>           \|/             \|/
  O   O   O   O   O   O   O   O 
  </code></pre>
  
  <p>LED output when raddr == 1</p>
  
  <pre><code>       \|/ \|/         \|/ \|/
  O   O   O   O   O   O   O   O 
  </code></pre>
  
  <p>I would think that address 1 in 512x8 mode would be the second 8 bits from RAM, which is <code>8'h22</code> or <code>8'b0010010</code>. Instead I get <code>8'h33</code> or <code>8'b00110011</code>. After a little experimentation, this seems to be the lower 8 bits of a 16 bit read.</p>
  
  <p>I'm not sure where I went wrong. Any help understanding what's going on here would be appreciated. Thanks!</p>