The index created by a generate loop or array if instances cannot be accessed via a dynamic variable. This restriction is mainly because of Verilog parametrization and a few other features that has the potential to make each instance unique. In a regular array, every element is the exact same type and has the exact same sub-components. The compiler must check at elaboration, not at run-time if the full path is valid.
There are several ways to get around this limitation. If you need to call all instances at once, you can create a generated loop of always blocks that wait for a trigger when you want to call all the tasks
event call_task1_name;
event call_function2_name;
genvar index;
for(index = 0; i < `WIDTH; i++) begin
always @call_task1_name
top.dram_model.GEN_MEM_MODEL.MEM_MODEL_INST[ index].i_dram0.task1_name;
always @call_function2_name
top.dram_model.GEN_MEM_MODEL.MEM_MODEL_INST[ index].i_dram0.function2_name;
end
When you trigger call_task_name, all the tasks will be called in parallel.
If you need to selectively call the tasks, then the best thing to do is wrap the calls in methods of a class
virtual class mem_model_array_c;
pure virtual task task1_name;
pure virtual function void function2_name;
endclass
mem_model_array_c mem_model_array_h[`WIDTH];
genvar index;
for(index = 0; i < `WIDTH; i++) begin :class_decl_loop
class mem_model_element_c extends mem_model_array_c;
task task1_name;
top.dram_model.GEN_MEM_MODEL.MEM_MODEL_INST[ index].i_dram0.task1_name;
endtask
function void function2_name;
top.dram_model.GEN_MEM_MODEL.MEM_MODEL_INST[ index].i_dram0.function2_name;
endfunction
endclass
mem_model_element_C mem_model_element_h = new();
initial mem_model_array_h[ index] = mem_model_element_h;
end : class_decl_loop
Now you can individually call the task or function with
mem_model_array_h[ expr].function2_name;
P.S. Please try to use parameters in packages instead of `defines for WIDTH
Please use function void for routines that do not consume time. Tasks can call other functions, but functions cannot call other tasks.