avl._core.memory module

class avl._core.memory.Memory(width: int = 32)[source]
__init__(width: int = 32) None[source]

Initialize the memory model.

Parameters:

width (int) – Width of the memory in bits (default is 32).

Raises:

ValueError – If width is not a positive integer.

set_init_fn(fn: Callable) None[source]

Set the initialization policy for the memory

Defined as a lambda function that takes an address and returns a value.

Parameters:

fn (callable) – Function to initialize memory at a given address.

Raises:

ValueError – If fn is not callable.

Example:

memory.set_init_fn(lambda address: 0)

set_endianness(endianness: Literal['little', 'big']) None[source]

Set the endianness of the memory (little / big).

Parameters:

endianness (str) – Endianness, can be ‘little’ or ‘big’.

Raises:

ValueError – If endianness is not ‘little’ or ‘big’.

add_range(start: int, end: int) None[source]

Add a memory range.

Parameters:
  • start (int) – Start address of the memory range.

  • end (int) – End address of the memory range.

Raises:

ValueError – If start address is not less than end address.

miss(address: int) None[source]

Action to be taken when a memory miss occurs. This method raises a ValueError indicating that the address was not found in memory. Expect user to override this method to implement custom behavior.

Parameters:

address (int) – Address that caused the miss.

Raises:

ValueError – Always raised to indicate a memory miss.

read(address: int, num_bytes: int | None = None, rotated: bool = False) int[source]

Read a value from the memory at the specified address.

Calls miss() if the address is not found in memory.

Rotate setting determines if the data should be rotated within the memory line Non-rotated data will be returns num_bytes from bit0 of the return data Rotated data will be returned from the offset within the memory line. i.e. a LE 4B read from address 0x1 on a 4B memory where each byte contains its address reads 0x03020104

Parameters:
  • address (int) – Address to read from.

  • num_bytes (int) – Number of bytes to read

  • rotated (bool) – Rotate data based on offset

Returns:

Value at the specified address.

Return type:

int

write(address: int, value: int, num_bytes: int | None = None, strobe: int | None = None, rotated: bool = False) None[source]

Write a value to the memory at the specified address.

Calls miss() if the address is not found in memory.

Rotated setting determines if the data is rotated within the memory line Non-rotated data will write num_bytes from bit0 to the memory Rotated data will write from the offset within the memory line.i.e. a LE 4B write of 0x03020100 to address 0x1 on a 4B memory writes: mem[1] = 0x01 mem[2] = 0x02 mem[3] = 0x03 mem[4] = 0x00

Parameters:
  • address (int) – Address to write to.

  • value (int) – Value to write.

  • num_bytes (int, optional) – Number of bytes to write (default is width // 8).

  • strobe (int, optional) – Strobe signal

  • rotated (bool) – Rotate data and strobe based on offset

export_to_file(filename: str, fmt: str | None = None) None[source]

Export memory contents to a file.

If fmt is not specified, it will be inferred from the file extension.

Parameters:
  • filename (str) – Path to the file where memory contents will be saved.

  • fmt (str, optional) – Format of the file, can be ‘vhex’ or ‘vbin’.

Raises:

ValueError – If format is not supported.

import_from_file(filename: str, fmt: str | None = None) None[source]

Load memory contents from a file.

If fmt is not specified, it will be inferred from the file extension.

Parameters:

filename (str) – Path to the file containing memory contents.

Raises:

FileNotFoundError – If the file does not exist.