BleuLlama / TinyBasicPlus

A C implementation of Tiny Basic, with a focus on support for Arduino
401 stars 117 forks source link

Option to align program lines to even memory addresses #39

Closed guruthree closed 6 years ago

guruthree commented 6 years ago

I wanted to get a basic interpreter running on an WeMos D1 ESP8266-based board, and when I saw https://github.com/BleuLlama/TinyBasicPlus/issues/35 was encouraged. However, when running TinyBasicPlus I encountered this error:

Exception (9):
epc1=0x40202c51 epc2=0x00000000 epc3=0x00000000 excvaddr=0x3fff625d depc=0x00000000

ctx: cont 
sp: 3fff73c0 end: 3fff75b0 offset: 01a0

>>>stack>>>
3fff7560:  3fff6560 3ffee7a8 3ffee798 40202c4a  
3fff7570:  00002580 0000000a 3fff6560 40202974  
3fff7580:  feefeffe 00000000 3fff6560 3fff6584  
3fff7590:  3fffdad0 00000000 3fff657d 40203974  
3fff75a0:  feefeffe feefeffe 3fff6590 40100108  
<<<stack<<<

and others like it. An Exception 9 is apparently a "LoadStoreAlignmentCause: Load or store to an unaligned address" error. This error was being caused by dereferencing the short int binary-format program line numbers (the 10 part of 10 PRINT A). After a bit of Googling I found https://readmodifywrite.github.io/blog/html/2016/12/15/2016_12_15_memory_alignment_esp8266.html which suggested the error was because the memory address was unaligned, which in this case translated to an odd numbered address. To prevent crashes and let TinyBasicPlus run on this particular ESP8266 chip, I've created a patch that when #define ALIGN_MEMORY 1 is selected, ensures that all line numbers are written to even addresses (are aligned).

BleuLlama commented 6 years ago

Looks great! Thanks!