Search code examples
dml-lang

How can i iterate over banks in DML 1.4?


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

Solution

  • 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.