Makogan
Posts: 71
Joined: Tue May 16, 2017 9:17 pm

Why is a stub function changing the behaviour of my program?

Mon Jun 12, 2017 9:30 pm

Ok this will sound like I am crazy, an idiot or both. I am attempting to make a "print" library for my embedded program. So I got a hard coded ascii font (8x8 bits) to render it. I started playing around with the idea of scaling the text up since 8x8 bits is a little small for my current resolution, then weird things started to happen.

This is exactly what is going on right now. As a test I am first drawing a white rectangle, and then on top of that a smaller cyan rectangle.

If I call no functions in between, I get the expected behaviour.
If I call a blank function (a stub, no arguments, no implementation) there are 2 possibilities.
If a variable is 11 or less I get the expected behaviour.
if a variable is 12 or more I only get the rectangle drawn before calling the stub.

In other words, making a variable have a value greater than 11 seems to change the machine state in such a way I cannot draw anymore. The function is:

Code: Select all

drawChar(){}

void init_display()
{
  Display_info disp = {1360, 768, 1360, 768, 32};

  set_init_display_message(&disp);

  write_to_mailbox((uint32_t) &mailbox_message | BUS_MASK, (Channel)(PTAG_ARM_TO_VC));
  read_from_mailbox(PTAG_ARM_TO_VC);

  while(mailbox_message[FB_RESPONSE] == RESPONSE_ERROR){}

  uint32_t size = 11;

  size = 13;\\ If this is >11 things don't work
  for(uint32_t i=0; i<CHAR_BITS*size; i++)
  {
    for(uint32_t j=0; j<CHAR_BITS*size; j++)
    {
      *(volatile uint32_t *)((mailbox_message[FB_PTR] & ~BUS_MASK) 
        + (i*mailbox_message[FB_VIRTUAL_WIDTH] + j)*mailbox_message[FB_DEPTH]/CHAR_BITS) = WHITE_32;//charTest[i*CHAR_BITS*size+j];
    }
  }

  const char *test = basic_font['A'];
  uint32_t charTest[CHAR_BITS*CHAR_BITS*size*size];
  drawChar(); //<- dark magic blank function that breaks things 

  size = 10;
  for(uint32_t i=0; i<CHAR_BITS*size; i++)
  {
    for(uint32_t j=0; j<CHAR_BITS*size; j++)
    {
      *(volatile uint32_t *)((mailbox_message[FB_PTR] & ~BUS_MASK) 
        + (i*mailbox_message[FB_VIRTUAL_WIDTH] + j)*mailbox_message[FB_DEPTH]/CHAR_BITS) = CYAN_32;//charTest[i*CHAR_BITS*size+j];
    }
  }
}
Has anyone experienced something like this before?

UPDATE:
It is related to the line: uint32_t charTest[CHAR_BITS*CHAR_BITS*size*size];
Commenting that line out seems to fix the problem. I assume there is a problem with this form of dynamic initialisation?

LdB
Posts: 437
Joined: Wed Dec 07, 2016 2:29 pm

Re: Why is a stub function changing the behaviour of my prog

Tue Jun 13, 2017 2:11 am

Smells like a roll out and overflow problem with the multiply

What is the value and type of CHAR_BITS?
Can't help unless we have all the details.

You could also be nice and help the optimizer out, there is lots of repeated mathematics which you are relying totally on the optimizer recognizing to factor them out. Even just factoring CHAR_BITS*size to a temp variable would help it as I see it 6 times.

uint32_t temp = CHAR_BITS*size;
is calling out to you and saves you some typing in the code and RSI in later life :-)

So I can see you are looking to do your own OS which I fully encourage.

What I will also say is it is very slow and messy to scale bitmap fonts. If you just want to scale the system font with screen res, the display of each character will be a heck of a lot faster to just include/link different font files based on screen res. If you do run the scaling and hold the result you don't want to have to scale on the fly every letter.

If you really want to nice scalable fonts go to something nicer like TTF fonts. The mathematics is a bit harder but some nice people on the net might give you the code for it :-)
https://www.codeproject.com/Articles/11 ... -Pi-Part-I
It is also backed up a opensource development
https://www.freetype.org/

Don't re-invent the wheel and "borrow" from others ;-)

Makogan
Posts: 71
Joined: Tue May 16, 2017 9:17 pm

Re: Why is a stub function changing the behaviour of my prog

Tue Jun 13, 2017 6:11 pm

Hello, I think the problem was related to memory, once I declared the array as a global variable and statically initialize it it went away. About the fonts. i actually have coded bezier implementations of TTF fonts, before, but my printing methods right now are simply for debugging, scaling was simply so that I would not go blind trying to read my own output.

Also char bits is just plain 8

Return to “Bare metal”

Who is online

Users browsing this forum: No registered users and 1 guest