Safe Memory Region Copy - Memory Operaitons

Microsoft

Problem Description

You are writing a memory model for a DV scoreboard. The model is a flat associative array: byte mem[int].

Implement copy_region() inside MemModel. It copies num_bytes bytes starting at src_addr into the region starting at dst_addr.

source and destination windows can also overlap.

Constraints / Notes

  • Do not allocate a temporary buffer — solve it with direction logic alone.

Implement copy_region() inside MemModel.

It should copy num_bytes consecutive bytes starting at src_addr and place them at dst_addr — all within the same mem[] array.

systemverilog

function void copy_region(int src_addr, int dst_addr, int num_bytes);

Inputs

Parameter

Type

Meaning

src_addr

int

Starting address to read from

dst_addr

int

Starting address to write to

num_bytes

int

How many bytes to copy

Example :

src_addr=2, dst_addr=4, num_bytes=4  (bytes 2,3,4,5 → 4,5,6,7)
Overlap! dst > src, so copy backwards: index 5→4→3→2
Without this, writing dst[4]=src[2] first would overwrite src[4]
before it is read.

Tips :

  • Copy direction must be chosen based on whether regions overlap and which address is higher.

  • If dst_addr <= src_addr or regions do not overlap: copy forwards (low index to high).

  • If dst_addr > src_addr and regions overlap: copy backwards (high index to low).

SystemVerilog

Code Editor Access

Log in to write, run and submit your solution to this interview question. It's free.

Output

Output goes here...