Electronic – Why can’t I print integer using Nios

cfpgaprocessor

So, my goal is to print (on the standard output) a number using a Nios II system on a FPGA Cyclone II Altera.

MY STEPS:

1) I created the following Nios II system with the Qsys tool (Quartus II):

(NIOS II CPU – JTAG – ONCHIP MEMORY RAM – SYS ID)

enter image description here

2) I wrote the following C code:

#include <stdio.h>
int main(){
    char a[13]="Hello world!";
    printf("%s\n", a);
}

3) I downloaded the system on the FPGA and I compiled and loaded my code using Altera Monitor Program, I ran the code and it works, my code prints "Hello world" on the Monitor Program terminal.
enter image description here

4) So, I tried to print a number and it fails 🙁

  • The code:

    #include <stdio.h>
    int main(){
        int a=3;
        printf("%i\n", a);
    }
    
  • The error:

    enter image description here

NOTES:
I tried to change %i with %d %u

Why it doesn't work?

Best Answer

Based on the verification error and looking at your Qsys system, there isn't enough RAM.

In Qsys, your RAM goes from 0x4000 to 0x7FFF. However when the JTAG programmer tries to upload the Nios firmware, it tries to verify 0x4000 all the way up to 0xE693. That means the size of your compiled code is a full 0x6694 (26260) bytes larger than the RAM.

By using %i,d or u, you are forcing the compiler to include the full routines for integer to string conversion, which in turn requires division and modulus code. This will use a fair amount of space. Much more so than %s which requires little code.