1

In DML 1.2 it's possible to use parameter banks to iterate over banks in device:

foreach node_bank in (banks) {
    <some logic>
}

But in DML 1.4 it produces error:

error: undefined value: 'undefined'

I tried to create a list with all banks in device to iterate over it:

param banks_list = [bank1, bank2, bank2]

but i got an error:

error: unknown identifier: 'bank1'

I also tried to create a session variable with banks (like it described here: Cannot use variable index in a constant list):

session bank banks_array = {bank1, bank2, bank3}

but i also got an error:

error: invalid data initializer: compound initializer not supported for type trait bank
odenysex
  • 25
  • 2

1 Answers1

1

In DML 1.4, you can use foreach X in (each T in (N)) to iterate over sets of objects of a certain template type inside a certain namespace. See the documentation for each ... in. You can use the dev as the namespace to get all objects in the entire device model.

You can use #foreach X in ( [list] ) to iterate over a compile-time list.

For example:

dml 1.4; 

// Test how to collect a set of banks to iterate over
// Have to put a template on each of them
template countable_bank {
    // Empty, just give us a type
}

template dummy_bank is bank {
    param register_size = 8;
    register a @ 0x00 "Dummy register";
    register b @ 0x08 "Dummy register to be non-zero";
}

bank bb1 is (countable_bank, dummy_bank) ;

bank bb2 is (countable_bank, dummy_bank) ;

bank bb0 is (dummy_bank) ;

bank bb3 is (countable_bank, dummy_bank) ;

method init_bb_banks () {
    local uint32 bcount = 0;

    foreach b in (each bank in (dev)) {
        bcount++;
    }

    local uint32 ccount = 0;
    foreach c in (each countable_bank in (dev)) {
        ccount++;
    }

    // Static list used with #foreach statement 
    local uint32 dcount = 0;
    #foreach d in ([bb0, bb1, bb2, bb3]) {
        dcount++;
    }

    log info, 1: "Found %d banks in device in total", bcount;
    log info, 1: "Found %d countable banks", ccount;    
    log info, 1: "Listed %d banks", dcount;    
}

In this example, init_bb_banks will count 4 banks overall and 3 countable banks, as there are three banks with the countable_bank template set. Since all banks inherit the bank template, you iterate over all banks by doing each bank in (dev).

The fixed list will count the 4 banks listed.

jakobengblom2
  • 5,531
  • 2
  • 25
  • 33
  • `#foreach d in ([bb0, bb1, bb2, bb3])` works for me fine, but `foreach b in (each bank in (dev))` doesn't work - i tried, but i got errors. – odenysex Mar 30 '23 at 11:42
  • What fails with `each bank in (dev)`? – jakobengblom2 Apr 01 '23 at 07:56
  • i wrote this code: `foreach b in (each bank in (dev)) { b.some_method() }` and i got error error: `'b' has no member named 'some_method'`. but when i changed i to `#foreach b in ([b1,b2,b3]) { b.some_method() }` it works – odenysex Apr 03 '23 at 06:19
  • That is because `some_method` is not part of the template type of `bank`. So the template-based iteration really works best when you have a template that provides some aspect of the bank's behavior that you want to invoke for all instances of it. Not as a way to get at anything in the bank. – jakobengblom2 Apr 03 '23 at 09:19