question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

loadMemoryFromFile() should place readmemh() inline for better compatibility

See original GitHub issue

loadMemoryFromFile() creates a separate file and uses a SV bind statement. A simple example:

import chisel3._
import chisel3.util.experimental.loadMemoryFromFile

class Foo(val bits: Int, val size: Int, filename: String) extends Module {
  val io = IO(new Bundle {
    val nia = Input(UInt(bits.W))
    val insn = Output(UInt(32.W))
  })

  val memory = Mem(size, UInt(32.W))
  io.insn := memory(io.nia >> 2);
  loadMemoryFromFile(memory, filename)
}

object FooObj extends App {
  chisel3.Driver.execute(Array[String](), () => new Foo(32, 1024, "insns.hex"))
}
# cat Foo.Foo.memory.v
module BindsTo_0_Foo(
  input         clock,
  input         reset,
  input  [31:0] io_nia,
  output [31:0] io_insn
);

initial begin
  $readmemh("insns.hex", Foo.memory);
end
                      endmodule

bind Foo BindsTo_0_Foo BindsTo_0_Foo_Inst(.*);

Yosys doesn’t like this, and likely there are other tools that don’t either. Is there any reason we don’t just place it inline?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:18 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
carlosedpcommented, Mar 9, 2021

@antonblanchard check out https://github.com/chipsalliance/firrtl/pull/2107. I’ve added a new annotation allowing inline readmem in Verilog emiter.

Your sample code would become:

import chisel3._
import chisel3.experimental.{ChiselAnnotation, annotate}
import firrtl.annotations.{MemoryFileInlineAnnotation}


class Foo(val bits: Int, val size: Int, filename: String) extends Module {
  val io = IO(new Bundle {
    val nia = Input(UInt(bits.W))
    val insn = Output(UInt(32.W))
  })

  val memory = Mem(size, UInt(32.W))
  io.insn := memory(io.nia >> 2);
  annotate(new ChiselAnnotation {
    override def toFirrtl = MemoryFileInlineAnnotation(memory.toTarget, filename)
  })
}

object FooObj extends App {
  chisel3.Driver.execute(Array[String](), () => new Foo(32, 1024, "insns.hex"))
}

that would generate the appropriate:

initial begin
  $readmemh("insns.hex", memory);
end

It would allow also configuration for binary files instead of hex, like:

import firrtl.annotations.{MemoryFileInlineAnnotation, MemoryLoadFileType}
...
  annotate(new ChiselAnnotation {
    override def toFirrtl = MemoryFileInlineAnnotation(memory.toTarget, "filename.bin", MemoryLoadFileType.Binary)
  })
...

and output

initial begin
  $readmemb("filename.bin", memory);
end
0reactions
carlosedpcommented, Nov 24, 2021

Opened #2260 to address this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

loadMemoryFromFile() should place readmemh() inline for ...
loadMemoryFromFile() should place readmemh() inline for better ... This should be compatible with the memory inference pass in yosys.
Read more >
loadMemoryFromFile - chisel3 3.3.3 - Chisel/FIRRTL
loadMemoryFromFile is an annotation generator that helps with loading a memory from a text file. This relies on Verilator and Verilog's $readmemh or ......
Read more >
Initialize Memory in Verilog | Project F: FPGA Dev
As demonstrated by this example, the memory array can have more entries than the data file. Yosys. Yosys automatically picks up $readmemh and...
Read more >
ZiLOG Developer Studio II--Z8 User Manual
readmem. Toggles load memory from file on debugger startup. TRUE, FALSE ... can be generated in-line for more efficient or generic code.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found