Odd memory problem mega 2560

I am interfacing with an old mixing console. It has a z80 that handles switching on the channel strips. Switches control LEDs and audio switching.

The z80 is now due for a peaceful retirement - I have built a replacement using at present a mega 2560 totally replacing the whole z80 board. I have sucessfully connected to the console hardware - a brief overview of the console hardware:

8 motherboards
8 channels per motherboard
4 CS (Chip select) lines per channel
3 8bit PIA ports per CS

the PIA ports are a mix of buttons that are read, or latches to set the relevant switches.

I hold a bool map of the bits in each port in an array:

bool consoleDataTree[TOTAL_MOTHERBOARDS][MOTHERBOARD_CHANNELS][NUMBER_OF_CS_LINES][PIA_PORTS][8] = {false};

this is a bool array, so a calculation says that is 6,144 bits of memory used.
Before I add the next function, my sketch compiles fine at 30% dynamic memory - there is already allot of code.

Next is my command map function it will have about 50 different commands in it. It is called from a script that analyses any button press on the console hardware - i will also use it for a new keypad controller and midi. Even with just these few currently commented lines - it fails to complile and reports 130% dynamic memory use....



void commandSendData(uint8_t MB, uint8_t CH, uint8_t  CS, uint8_t PORT, uint8_t BIT, uint8_t commandId)
{
  consoleDataTree[MB][CH][CS][PORT][BIT] = !consoleDataTree[MB][CH][PORT][BIT];   // toggle the switch state - toggle the button event in memory

    switch (commandId)
  {
    // INPUTS
    case 0:

      break;
    // GROUPS
    case COMMAND_GROUP_BUTTON_GROUP_MUTE:       // write TO the relevant PIA ports - update the relevant bits

//      consoleDataTree[MB][CH][1][0][2] = consoleDataTree[MB][CH][CS][PORT][BIT];  // SWITCH GROUP MUTE - this compiles at 30%
//      consoleDataTree[MB][CH][1][0][3] = consoleDataTree[MB][CH][CS][PORT][BIT];  // LED GROUP MUTE - compiles at 30%
      // send to pia
//      writeToPiaPort(MB, CH, 1, 0);   // this compiles at 67%
      break;
  }
}

with all three uncommented it reports 130%. Im scratching my head.

Type 'boolean' takes up one byte each. That means 6144 BYTES of memory.

You could store each port (eight bits) as a byte:

byte consoleDataTree[TOTAL_MOTHERBOARDS][MOTHERBOARD_CHANNELS][NUMBER_OF_CS_LINES][PIA_PORTS] = {false};

That would get you back down to 768 bytes.

doh. I saw somewhere it was a bit, but i see you are right.... I will do that right now.

but its odd is it not that it compiles happily with the array declared. It compiles but only falls over when there are multiples uncomented. I would like to understand why this is happening.

The compiler is very smart. If you only use a few entries in a large array, it just throws away the values you don't use. That save a lot of memory space. :slight_smile:

Is this intentional?

yes. as a button is pressed, it toggles the switch. This is my toggle.

I see

Indexing does not match, right and left. So, whatever that line does, it does not appear to simply "toggle".

well spotted