Something went wrong. Try again.
A small RV32 SoC
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130# pyright: reportInvalidTypeForm=false
from amaranth import *from amaranth.lib import wiringfrom amaranth.lib.wiring import In, Outfrom amaranth.lib.memory import Memoryfrom amaranth.sim import Simulator, SimulatorContext, Period
from buses import PicoBus
class SimpleROM(wiring.Component): bus: In(PicoBus())
def __init__(self, rom_size=512, rom_init=[]): super().__init__()
self.rom_size = rom_size self.rom_init = rom_init
def elaborate(self, platform): m = Module()
m.submodules.rom = rom = Memory(shape=32, depth=self.rom_size, init=self.rom_init) read_port = rom.read_port()
with m.FSM(): with m.State("INIT"): m.d.comb += self.bus.mem_ready.eq(0) with m.If(self.bus.mem_valid): m.next = "RESPOND"
with m.State("RESPOND"): m.d.comb += self.bus.mem_ready.eq(1) m.next = "INIT"
m.d.comb += [ read_port.en .eq(self.bus.mem_valid), read_port.addr .eq(self.bus.mem_addr), self.bus.mem_rdata .eq(read_port.data) ]
return m
# NOTE: This implementation might not be the best# however, It responds in a single cycleclass SimpleRAM(wiring.Component): bus: In(PicoBus())
def __init__(self, ram_size=4096, ram_init=[]): super().__init__()
self.ram_size = ram_size self.ram_init = ram_init
def elaborate(self, platform): m = Module()
# TODO: Proper debug signals m.submodules.ram = self.ram = ram = Memory(shape=32, depth=self.ram_size, init=self.ram_init) read_port = ram.read_port() write_port = ram.write_port(granularity=8)
with m.FSM(): with m.State("INIT"): m.d.comb += self.bus.mem_ready.eq(0)
with m.If(self.bus.mem_valid): with m.If(self.bus.mem_wstrb == 0b0000): m.d.comb += [ read_port.en.eq(1) ]
with m.Else(): m.d.comb += [ write_port.en .eq(self.bus.mem_wstrb), write_port.data.eq(self.bus.mem_wdata) ]
m.next = "WAIT"
with m.State("WAIT"): m.d.comb += self.bus.mem_ready.eq(1) m.next = "INIT"
m.d.comb += [ read_port.addr .eq(self.bus.mem_addr[2:32]), write_port.addr .eq(self.bus.mem_addr[2:32]), self.bus.mem_rdata .eq(read_port.data) ]
return m
def simulate(): dut = SimpleRAM() sim = Simulator(dut) sim.add_clock(period=Period(us=1))
async def simulation(ctx: SimulatorContext): for _ in range(5): await ctx.tick()
ctx.set(dut.bus.mem_valid, 1) ctx.set(dut.bus.mem_addr, 0x1) ctx.set(dut.bus.mem_wdata, 0xBEEF) ctx.set(dut.bus.mem_wstrb, 0b1111) await ctx.tick()
assert ctx.get(dut.bus.mem_ready) == 1, "Mem not ready" ctx.set(dut.bus.mem_valid, 0) await ctx.tick()
assert ctx.get(dut.bus.mem_ready) == 0, "Mem is still ready" ctx.set(dut.bus.mem_valid, 1) ctx.set(dut.bus.mem_wdata, 0) ctx.set(dut.bus.mem_wstrb, 0b0000) await ctx.tick()
ctx.set(dut.bus.mem_valid, 0) assert ctx.get(dut.bus.mem_ready) == 1, "Mem not ready for load" assert ctx.get(dut.bus.mem_rdata) == 0xBEEF, "Mem readback is not same"
sim.add_testbench(simulation)
with sim.write_vcd("ram.vcd"): sim.run()
if __name__ == "__main__": simulate()