Post Reply 
C translator for calculator project
05-10-2019, 08:47 AM (This post was last modified: 07-10-2019 06:05 AM by Dan.)
Post: #23
RE: C translator for calculator project
Thank you to everyone for taking the time to share your thoughts, I'm really enjoying reading your posts. KeithB summed it up in the previous post, my aim is to make my programs easier to edit and understand. This would be for small to medium programs, for big projects I would recommend an industrial IDE like Code Composer Studio, which is what I've been using to write the firmware (in C).

For example, here is my keystroke program to generate the Mandelbrot set:

Code:

    CLEAR GLCD
    0
    XCOORD<-
    0
    YCOORD<-
A   DROP 1 LEVEL
    -1
    0.03149606299
    YCOORD->
    *
    +
    CIMAG<-
    ZIMAG<-
B   DROP 1 LEVEL
    -2
    0.03149606299
    XCOORD->
    *
    +
    ZREAL<-
    CREAL<-
    DROP 1 LEVEL
    0
    COUNT<-
C   DROP 1 LEVEL
    ZREAL->
    X^2
    ZREAL2<-
    ZIMAG->
    X^2
    ZIMAG2<-
    +
    4
    GOTOIF> LBL D
    DROP 1 LEVEL
    ZREAL->
    ZIMAG->
    2 
    *
    *
    CIMAG->
    +
    ZIMAG<-
    DROP 1 LEVEL
    ZREAL2->
    ZIMAG2->
    -
    CREAL->
    +
    ZREAL<-
    DROP 1 LEVEL
    COUNT+1
    20
    GOTOIF< LBL C
    DROP 1 LEVEL
    XCOORD->
    YCOORD->
    PIXEL ON
    DROP 1 LEVEL
D   DROP 1 LEVEL
    CIMAG->
    ZIMAG<-
    DROP 1 LEVEL
    XCOORD+1
    128
    GOTOIF< LBL B
    DROP 1 LEVEL
    0
    XCOORD<-
    DROP 1 LEVEL
    YCOORD+1
    64
    GOTOIF< LBL A
    HALT EXECUTION
    DROP 2 LEVELS
    END PROGRAM

Variable names can be up to 8 characters long and are entered via the command line, as are numbers. This makes the programs shorter and easier to understand. Here is the C version - I think it is much more readable:

Code:


#define pixelWidth 0.03149606299

unsigned char iterations = 20;
unsigned char n;
bool inside;

double XCOORD;
double YCOORD;
double CIMAG;
double CREAL;
double ZREAL;
double ZIMAG;
double ZREAL2;
double ZIMAG2;

for(YCOORD = 0; YCOORD < 64; YCOORD++) {
    CIMAG = -1.0 + pixelWidth * YCOORD;

    for(XCOORD = 0; XCOORD < 128; XCOORD++) {
        CREAL = -2.0 + pixelWidth * XCOORD; 
        ZREAL = CREAL;
        ZIMAG = CIMAG; 
        inside = true;
        
        for(n = 0; n < iterations; n++) {
            ZREAL2 = ZREAL * ZREAL;
            ZIMAG2 = ZIMAG * ZIMAG;
            if (ZREAL2 + ZIMAG2 > 4) {
                inside = false;
                break;
            }
            ZIMAG = 2.0 * ZREAL * ZIMAG + CIMAG;
            ZREAL = ZREAL2 - ZIMAG2 + CREAL;  
        }
        if (inside) pixelOn(XCOORD, YCOORD);
    }
}

Another reason I chose C is because this is the language the firmware is written in. From the outset my goal (pipe dream?) was to make a calculator that others would use and contribute software to, and I thought that if people are using C on the calculator then they may be interested in contributing to the development of the firmware (it really is a big job for one person).

However considering how opposed people are to C (whoever thought choosing a calculator programming language could be such a contentious issue!), perhaps I should reconsider. Anyway, I have only used 138K of Flash (out of 1000K) and 15K of RAM (out of 256K), so there is always room for more than language (actually two, I'll keep the keystroke language).

BTW, the keystroke programming language is working well, 13 seconds to generate the Mandelbrot set on a 128x64 display with 20 iterations per pixel using doubles, and 7 seconds using floats and the floating-point coprocessor. At some stage I'll try the N queens program to see how well it performs compared to other calcs. Some pics I took while entering the above program:

[Image: 48246930467_2f94f5d54c_o.jpg]

[Image: 48246920512_b3f5d9ac9e_o.jpg]

[Image: 48246936967_8321cfba25_o.jpg]

[Image: 48246853466_f7233478bb_o.jpg]

[Image: 48246818276_07e00d4f14_o.jpg]
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
C translator for calculator project - Dan - 05-06-2019, 05:18 AM
RE: C translator for calculator project - Dan - 05-10-2019 08:47 AM



User(s) browsing this thread: 1 Guest(s)