Converting old BASIC command to Arduino, one help please?

Most of the BASIC command can easily be converted to C or Arduino IDE format but there is one that seems to elude me. It was originally written for Commodore 128 and it generated a complex 3D wireframe graphic. Back in the day it took the pokey 2MHz computer about 6 hours to complete and I wanted to do the same on my Uno with LCD module.

The code originally came out of "Microcomputer Graphics" by Roy E Myers and the one I had was originally for Apple II. I was able to convert them to work on Commodore 128 with a bit of changes in the code. But that was about 30 years ago when BASIC was still the best easy language to learn.

I can't remember how deffn() is used on Commodore computer and so I can't figure out if there's something similar with C that can be used.

PS it would be funny if there is a C code interprenter that lets me run BASIC code as originally done and let ATMega do the translating.

Perhaps if you posted this code we might get a bit further down the path of helping you?

The original code is still on my C128 and it would take a while to copy over the entire listing. I have not even started on the C, I was looking through BASIC figuring out what needs to be done here and there and deffn() is stumping me.

"deffn" was just an inline function definition, wasn't it?

Google is your friend; I found this: C64 wiki -> DEF

Sounds like functions to me.

Wouldn't it be easier to start from scratch on this, than to translate a 20th century relic?

Yes, it is pretty much "define function" For example, I give the function named example the function "x+3" so when I call function example(5), it will do the math and return 8. It was to simplify lengthy and complex math equation to a few letters in BASIC. But I don't know of a C equivalent of defining function.

defining function would likely be done in setup() to store equation in memory and calling them in loop() would recall the equation and process the math. What gets inside () is treated as variable x in the math equation. It can get complex such as "define example a+x" and a is a variable container. When I call example (3), it loads what is in a and add to 3.

Like spoken language, some things don't translate easily. I hope I don't need to do lengthy alternative listing the entire equation every single time. The original BASIC code was about 20k and depending on how everything converts to C, I may not have much space left in the 32k storage space. One of the function in the original code is nearly 30 characters long.

aarg:
Wouldn't it be easier to start from scratch on this, than to translate a 20th century relic?

Because I wanted to see how well ATMega328p performs compared to 30 years old 6500 series CPU. Both didn't have dedicated math processor. C128 runs max 2Mhz and costs a few hundred dollars. 328p has only 2k RAM but costs about $3.

defining function would likely be done in setup()

It most certainly would not.

You can't define a function inside another function (big hint there) in this dialect of C.

wilykat:
Because I wanted to see how well ATMega328p performs compared to 30 years old 6500 series CPU. Both didn't have dedicated math processor. C128 runs max 2Mhz and costs a few hundred dollars. 328p has only 2k RAM but costs about $3.

That would not be a fair comparison. BASIC is interpreted and C is compiled to machine language (for one thing). Also, the 328 lacks a keyboard and cheesy plastic case. :slight_smile:

wilykat:
defining function would likely be done in setup() to store equation in memory and calling them in loop() would recall the equation and process the math. What gets inside () is treated as variable x in the math equation. It can get complex such as "define example a+x" and a is a variable container. When I call example (3), it loads what is in a and add to 3.

You're hanging around here since 2012; I would think that you have a little bit of experience in C/C++. If not, get a book.

No offense intended.

wilykat:
Because I wanted to see how well ATMega328p performs compared to 30 years old 6500 series CPU. Both didn't have dedicated math processor. C128 runs max 2Mhz and costs a few hundred dollars. 328p has only 2k RAM but costs about $3.

A 328p at 16MHz would outperform a 6502 at 2MHz more than twentyfold (a good guess I think), a Mega2560 could use up to 64k RAM directly attached.

Whandall:
A 328p at 16MHz would outperform a 6502 at 2MHz more than twentyfold (a good guess I think), a Mega2560 could use up to 64k RAM directly attached.

A 2MHz 6502 would be a rarity. 1MHz was more typical.

C128 had a 6500 compatible variant (8502 to be exact) that ran at 2MHz and has 6502 mode when it runs in C64 mode.

... it generated a complex 3D wireframe graphic ...

Unless you are going down the nostalgia path (and you may well find a C128 emulator) surely this is an X-Y problem?

How about describing this 3D wireframe graphic, and we can see if there is some C++ code that generates it?

Even if somehow the Basic code could be translated into C++ you still need extra stuff to write to the LCD module.

VICE – the Versatile Commodore Emulator:
http://vice-emu.sourceforge.net

Indeed. And with that you can run the G-Pascal compiler that I wrote for the Commodore 64 in 1978.

Sheesh; the way people go off on tangents...

A basic statement like "DEF FN DIST(x, y) = sqrt( x^2 + y^2)" could become a C function like:

float dist(float x, float y) {
   float temp = x^2 + y^2;
   return sqrt(temp);
}

Defined at "top level" (outside of any functions) in your C source code.
Please tell me you're not making use of the apparent ability of BASIC to redefine the function at runtime - that would be ... more difficult.

I may not have much space left in the 32k storage space. One of the function in the original code is nearly 30 characters long.

Since C is a compiled language, whatever you write will be condensed to near-minimal code size, even if you use lots of extra characters, and long variable names, and "extra" intermediate steps, and comments. I could have written the above sample C code as:

float dist(float x, float y) { return sqrt(x^2+y^2); }

but it probably wouldn't run any faster.

(for those not familiar with "the bad old days", most of the BASICs are early personal computers with parse and interpret the SOURCE code when they were run. So you could actually make your program run faster by using shorter variable names, leaving out all comments, and scrunching things together without any formatting. Shudder.)

I can be more explicit given an example of the code you need to convert.

I would expect an Arduino sketch to run much faster than a PET BASIC program, implementing similar algorithms. But it might run into a RAM shortage. Early personal computers might have had "only" 32k of memory, but it was typically ALL RAM that held both code and data. And the Arduino interface to any particular display is likely to be slower than the built-in video of a PET.

westfw:
Sheesh; the way people go off on tangents...

I still think it's an X-Y problem. :stuck_out_tongue: