How to copy memory content from one address location to other location using SV Function /Dont use temporary variable use while copying?

I am trying to copy the memory address content from [0th- 4th] location to [3th -7th] address location . Need not use temporary variable while copying . I see there is overap for 6th and 7th location copied data and its incorrect data . How to fix this issue ? can we use NBA assignment /Fork_join concept to do that ? if yes how to do ?

Current Output

run -all
# Mem[          0] = 17750c26 
# Mem[          1] = 9d0db966 
# Mem[          2] = bcb9e3b6 
# Mem[          3] = 510e08c6 
# Mem[          4] = 83956e46 
# Mem[          5] = 3bd10f72 
# Mem[          6] = 769bf32e 
# Mem[          7] = fa374467 
# Mem[          8] = 3386553a 
# Mem[          9] = 46f91c6a 
# After copying - Mem[          0] = 17750c26 
# After copying - Mem[          1] = 9d0db966 
# After copying - Mem[          2] = bcb9e3b6 
# After copying - Mem[          3] = 17750c26 
# After copying - Mem[          4] = 9d0db966 
# After copying - Mem[          5] = bcb9e3b6 
# After copying - Mem[          6] = 17750c26 
# After copying - Mem[          7] = 9d0db966 
# After copying - Mem[          8] = 3386553a 
# After copying - Mem[          9] = 46f91c6a 
# Copy content of memory locations from one address to another address without using temp variables 

SV Code - 
class copy_memory ;
parameter DEPTH = 10 ;
logic [31:0] mem[DEPTH];

task swap( logic [31:0] addr_src , logic [31:0] addr_dest , logic [31:0] size );

// Intialise the memory 
for (int i=0 ; i< DEPTH ; i++) begin 
mem[i] = $urandom();
$display("Mem[%d] = %h ",i,mem[i]);
end 

for (int i=0 ; i< size ; i++) begin 
mem[addr_dest] = mem[addr_src] ;
addr_dest++;
addr_src++;
end 

for (int i=0 ; i< DEPTH ; i++) begin 
$display("After copying - Mem[%d] = %h ",i,mem[i]);
end 

endtask 

endclass

module mem_model ;

initial begin 
copy_memory mem_h;
mem_h = new();
mem_h.swap(0,3,5) ;
$display ("Copy content of memory locations from one address to another address without using temp variables ");
end 

endmodule
```verilog

Using NBAs would solve your problem if you do not need the results in the same time step. Otherwise you will need to test for an overlap requiring copying in reverse order.

if(addr_src < addr_dest && addr_src + size >= addr_dest) begin
   addr_dest += size;
   addr_src += size;
      repeat(size)
        mem[--addr_dest] = mem[--addr_src] ;
else begin
      repeat(size)
        mem[addr_dest++] = mem[addr_src++] ;
end
1 Like

Thanks @dave_59 . your solution works fine when we want to mem copy to happen at the same timestamp .
I have also tried using NBA and I see there is no overlap happening for mem locations. Pasting code as below -

using NBA assignments Logic -

  
  fork 
        for (int i=0 ;i<size ;i++)  begin 
          mem[addr_dest+i] <= mem[addr_src+i] ;
         
        end 
   join
  #1 ;
  
Output -

> KERNEL: Mem[          0] = 005444e2 
> KERNEL: Mem[          1] = 5d205fad 
> KERNEL: Mem[          2] = 6d8bf378 
> KERNEL: Mem[          3] = 34d69016 
> KERNEL: Mem[          4] = fdd88f7b 
> KERNEL: Mem[          5] = 24431c11 
> KERNEL: Mem[          6] = ef29a6e3 
> KERNEL: Mem[          7] = b2b609ae 
> KERNEL: Mem[          8] = cd3f19b8 
> KERNEL: Mem[          9] = 934e9689 
> KERNEL: After copying - Mem[          0] = 005444e2 
> KERNEL: After copying - Mem[          1] = 5d205fad 
> KERNEL: After copying - Mem[          2] = 6d8bf378 
> KERNEL: After copying - Mem[          3] = 005444e2 
> KERNEL: After copying - Mem[          4] = 5d205fad 
> KERNEL: After copying - Mem[          5] = 6d8bf378 
> KERNEL: After copying - Mem[          6] = 34d69016 
> KERNEL: After copying - Mem[          7] = fdd88f7b 
> KERNEL: After copying - Mem[          8] = cd3f19b8 
> KERNEL: After copying - Mem[          9] = 934e9689