How do I make a port of MicroPython for Casio calculators?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: How do I make a port of MicroPython for Casio calculators?

Post by stijn » Thu Aug 09, 2018 8:31 am

Build the unix or windows port and debug that step by step to see what gets executed. If mp_execute_bytecode doesn't get reached the problem should be in lexing, parsing or compiling. Hard to tell what. Could be a mismatch of options in mpconfigport, or something non-standard with the way the platform implements doubles. You could try other float implementations as well?

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Thu Aug 09, 2018 7:48 pm

Zezombye wrote:I can't put breakpoints so I did the "manual" method with good old print().

Anyway, I put print statements everywhere (beginning of mp_execute_bytecode, before for loop, beginning of for loop, beginning of switch...): when I type an integer, all print statements are executed, however when I type a double it crashes before it even prints anything.

So I guess the crash occurs before the execution of mp_execute_bytecode() :/

I remember now there was a second macro that needed to be enabled when I was playing around with this issue.

I’ll reply again once I get to my desk as I’m mobile right now.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Fri Aug 10, 2018 6:56 pm

Zezombye wrote:
Thu Aug 09, 2018 4:40 am
I can't put breakpoints so I did the "manual" method with good old print().

Anyway, I put print statements everywhere (beginning of mp_execute_bytecode, before for loop, beginning of for loop, beginning of switch...): when I type an integer, all print statements are executed, however when I type a double it crashes before it even prints anything.

So I guess the crash occurs before the execution of mp_execute_bytecode() :/
Interesting that it crashes before printing ANYTHING i.e. it's not printing the error message that an exception is throwing out; we know that exceptions work for you from the "name not defined" screenshot you gave us.

The function that parses floats is mp_parse_num_decimal().
Put print statements in there, specifically before and after any "new" statements i.e. mp_obj_new_float()
My guess is that you didn't initialize the heap correctly.

Zezombye
Posts: 34
Joined: Mon Jul 30, 2018 8:29 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by Zezombye » Sat Aug 11, 2018 4:35 am

Seems the pow() function is broken.

The problem comes from this line:

Code: Select all

dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val);
For input 1.0, dec_val is 10.0000000, exp_val is -1, and the result of pow(10, exp_val) is... -0.0000.
This seemingly happens for all inputs of pow().
Weirdly, if I remove my print() statements, it crashes again.

Something else is happening to the value, because after the calculation dec_val is -0.00000 but it prints some weird value like -4.316e-269.

I'll investigate, this is not a bug in micropython but with the libc I am using.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Mon Aug 13, 2018 2:50 pm

Zezombye wrote:
Sat Aug 11, 2018 4:35 am
Seems the pow() function is broken.

The problem comes from this line:

Code: Select all

dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val);
For input 1.0, dec_val is 10.0000000, exp_val is -1, and the result of pow(10, exp_val) is... -0.0000.
This seemingly happens for all inputs of pow().
Weirdly, if I remove my print() statements, it crashes again.

Something else is happening to the value, because after the calculation dec_val is -0.00000 but it prints some weird value like -4.316e-269.

I'll investigate, this is not a bug in micropython but with the libc I am using.
What happens if you define a dummy pow() inside the .c-file where it's used?

Zezombye
Posts: 34
Joined: Mon Jul 30, 2018 8:29 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by Zezombye » Thu Aug 30, 2018 9:12 pm

It just uses this function instead of the default one, why?

Anyway, I still haven't resolved this issue, but I do know it has absolutely nothing to do with MPy. Even a small program written from scratch has this error.

Why I'm posting is because I'm running into a memory error. I tried to get the available memory to MPy, but the script doesn't work because there is not enough available memory:

Code: Select all

def mem():
  try:
    l=[0]
    while True:
      try:
        l=l+l[l[0]:]
      except:
        if l[0]<len(l)-1:
          l[0]=len(l)-1
        else:
          print("+",4*len(l))
          l[0]=4*len(l)+mem()
          break
  except:
    return 0
  return l[0]
Summing gc.mem_free() and gc.mem_alloc() gives me 2064 bytes:
Image

As 2064 is very close to a power of 2, I think there is a hardcoded limit somewhere. I don't think there is a coincidence because 2 kb is quite low, I was expecting at least 5 kb (especially since there is 48 kb of heap total, there should be plenty of room, and MPy's website states that 16 kb is enough). However, I looked in the config files and gc.c and I didn't find anything.

Is there an hardcoded limit, or should I look to free memory?

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Thu Aug 30, 2018 9:17 pm

Zezombye wrote:
Thu Aug 30, 2018 9:12 pm
It just uses this function instead of the default one, why?

Anyway, I still haven't resolved this issue, but I do know it has absolutely nothing to do with MPy. Even a small program written from scratch has this error.

Why I'm posting is because I'm running into a memory error. I tried to get the available memory to MPy, but the script doesn't work because there is not enough available memory:

Code: Select all

def mem():
  try:
    l=[0]
    while True:
      try:
        l=l+l[l[0]:]
      except:
        if l[0]<len(l)-1:
          l[0]=len(l)-1
        else:
          print("+",4*len(l))
          l[0]=4*len(l)+mem()
          break
  except:
    return 0
  return l[0]
Summing gc.mem_free() and gc.mem_alloc() gives me 2064 bytes:
Image

As 2064 is very close to a power of 2, I think there is a hardcoded limit somewhere. I don't think there is a coincidence because 2 kb is quite low, I was expecting at least 5 kb (especially since there is 48 kb of heap total, there should be plenty of room, and MPy's website states that 16 kb is enough). However, I looked in the config files and gc.c and I didn't find anything.

Is there an hardcoded limit, or should I look to free memory?

You can use `gc` inside a script.

Code: Select all

num_free_bytes = gc.mem_free()
There's no hard-coded limit. The implementation of `list` doubles in size when the underlying memory is full

Code: Select all

if (self->len >= self->alloc) {
        self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
This is kind of a bug for the case when it fails; I'll submit a report.

Zezombye
Posts: 34
Joined: Mon Jul 30, 2018 8:29 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by Zezombye » Fri Aug 31, 2018 9:57 am

So part of it is a bug in micropython (because it is doubling the size of lists, consuming even more memory, when there is no memory left)?

Also, is there a way to make errors display the character? I just did a beginner error (using = instead of ==), but MPy doesn't show me exactly where it happened:
Image

I would like an output similar to this:
Image

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Fri Aug 31, 2018 6:28 pm

Zezombye wrote:
Fri Aug 31, 2018 9:57 am
So part of it is a bug in micropython (because it is doubling the size of lists, consuming even more memory, when there is no memory left)?
Calling it a bug may be too harsh. At the very least, doubling is not a good idea for super memory-constrained systems.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: How do I make a port of MicroPython for Casio calculators?

Post by jickster » Fri Aug 31, 2018 8:27 pm

Zezombye wrote:
Fri Aug 31, 2018 9:57 am

Also, is there a way to make errors display the character? I just did a beginner error (using = instead of ==), but MPy doesn't show me exactly where it happened:
Image

I would like an output similar to this:
Image
No. I'll create an issue for that.

https://github.com/micropython/micropython/issues/4090

Post Reply