Question Regarding $reverse() Function

Let’ say I have a ROM module like this:

module myROM (
input address[5],
output out[4],
)
{
const DATA = { 0,1,2,3,4,5,6,7,8,9,10 };
always {
out = DATA[address];
}

For this ROM, address 0 yields an out value of 10.

But suppose I want address 0 to output 0, i.e. read them is forward order instead of reverse. I could count the members: count = 11. Then I can use xAddr = (count-1) - address. Now xAddr will give me the values in the reverse order like I want. This works!

But there is a function that seems to do exactly that already: $reverse(a). I think I should be able to use $reverse(address) to get the result I want (without having to keep track of the number of items in the array), but it doesn’t seem to work. What is it I don’t understand here? Thank you!

$reverse() reverses the outermost dimension of an array.

You could use it here, but you would use it when defining DATA.

const DATA = $reverse({ 4d0,4d1,4d2,4d3,4d4,4d5,4d6,4d7,4d8,4d9,4d10 }); // DATA[0] is now 0

You could also use

out = DATA[$width(DATA)[0] - 1 - address] // for Lucid V2
out = DATA[DATA.WIDTH[0] - 1 - address]; // for Lucid V1

This way you don’t need to think about the length of DATA. It’s width value will be an array like { 4, NUM_OF_ELEMENTS } so $width(DATA)[0] is NUM_OF_ELEMENTS and $width(DATA)[1] is 4 (since I set the elements in the array to be 4 bits wide).

Thank you very much, Justin!!!