ANSI C Compatibility

Discussion of issues related specifically to writing code for native mode devices. This includes ZBasic code as well as assembly language code and C code, both inline and standalone.
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

ANSI C Compatibility

Post by liam.zbasic »

Is it possible to combine ANSI C code with Zbasic? The intent is to take advantage of Matlab autocode for complex control systems. Comments appreciated.

P.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: ANSI C Compatibility

Post by dkinzer »

liam.zbasic wrote:Is it possible to combine ANSI C code with Zbasic?
Yes but only with native mode devices, e.g. ZX-24n.

You can use inline C code within ZBasic source code, separate C source modules, compiled object modules and/or object libraries.
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

Excellent. Is all the capability you outlined administered by the Zbasic IDE? Do you have a simple "hello world" example using separate C source modules? Thank you for your quick response.

P.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:Do you have a simple "hello world" example using separate C source modules?
The attached .zip file contains a simple project containing one ZBasic module (c_source.bas) and a C source module (factorial.c). It is somewhat contrived since the functionality implemented in C could just as easily have been done in ZBasic but it illustrates the technique nonetheless.

The content of c_source.bas is reproduced below. The key to making a call to a function implemented in C is to prepare a Declare statement that informs the ZBasic compiler as to the parameters and type of the C function. The Alias clause tells the compiler the actual name of the C function being called. The declared name (used in ZBasic code) needn't be the same. For more information, see the ZBasic Reference manual section Defining and Using External Subroutines, Functions and Variables

Code: Select all

Declare Function Factorial(ByVal val as UnsignedInteger) as UnsignedInteger Alias "factorial"

Sub Main()
  Dim val as UnsignedInteger
  val = 5
  Debug.Print val; "! is "; Factorial(val)
End Sub
The content of the module factorial.c (reproduced below) is a straightforward recursive implementation of the factorial function.

Code: Select all

unsigned
factorial(unsigned val)
{
  return&#40;&#40;val <= 1&#41; ? 1 &#58; factorial&#40;val - 1&#41; * val&#41;;
&#125;
When working with external C modules, object modules or object libraries, it may be a good idea to add the compiler option --verbose to the project file (before the module names). With that option present, the output from the backend compiler and linker will be displayed in the ZBasic IDE output window so you'll be able to see error messages that are issued. Without that, the ZBasic compiler will simply report that an error occurred during the build without any clues as to what the error might be.

This technique is recommended for more experienced programmers and, particularly, those with experience programming in C.
Attachments
c_source.zip
ZBasic example using a C source module.
(562 Bytes) Downloaded 3426 times
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

The reference manual states that for native mode MCUs, "...the compiler first produces equivalent C code corresponding to the subroutines, functions and data definitions..." Questions... is the compiler related to WINAVR GCC or Elba Corp specific? If Elba Corp, where can I find documentation for the MCU related functions? Also, is there a speed advantage over all C code vs. visual basic + C? I'm considering the zx-40n device. Thank you.
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

liam.zbasic wrote:Questions... is the compiler related to WINAVR GCC or Elba Corp specific?
The compiler (strictly speaking toolchain) used is GNU that includes AVR-GCC, AVR-OBJCOPY etc.

WinAVR is just a neat way to package the GNU toolchain for AVR and other tools for a Windows platform. If you are using Linux then you need to do a bit more work in terms of installing everything.
liam.zbasic wrote:Also, is there a speed advantage over all C code vs. visual basic + C? I'm considering the zx-40n device.
There are two types of devices, ZBasic Virtual Machine (ZVM) and native.

For the VM devices your ZBasic program is compiled into an intermediate format (Virtual Machine language) that is then downloaded and executed by the ZBasic Virtual Machine.

For native mode devices your ZBasic program is compiled into C code. That C code is then built (under the covers) by the AVR GNU toolchain into an executable of AVR machine instructions. When this executable is downloaded, it is stored into the AVR flash and executed just like any AVR program.

As Visual Basic (ZBasic) + C becomes all C code, there is no distinct performance advantage over writing all C. It is always possible to tune code but that takes a lot more effort, often for little benefit. The draw of the ZBasic platform is the huge library of functions and the easy to use programming environment that gets you started very quickly.

I would just like to point out that there are a large variety of ZVM and native mode devices to choose from. For native mode, there are the following devices in order of increasing power and function: ZX-328n, ZX-24n, ZX-24ne, ZX-128ne, ZX-1281n, ZX-1281ne, and ZX-1280n.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:is the compiler related to WINAVR GCC or Elba Corp specific?
For native mode, the ZBasic compiler comprises two distinct components. The "front end" is code written by us. It translates your ZBasic code to equivalent C code and then invokes the "back end" compiler to produce native AVR code from the C code. We use a specific version of the avr-gcc compiler (currently, the one provided with the WinAVR 20071227 release) but it has not been modified in any way.
liam.zbasic wrote:If Elba Corp, where can I find documentation for the MCU related functions?
I'm not sure what you're really asking here. Native mode ZBasic applications can use (most of) the same set of ZBasic System Library routines as VM mode ZBasic applications. The documentation for all of those routines can be found in the ZBasic System Library Manual. If you're referring to capabilities of the underlying AVR processor that may or may not be supported by ZBasic System Library routines, you'll have to refer to the Atmel datasheet for the specific underlying AVR processor to get more details about how the capabilities are accessed. You can download Atmel AVR datasheets for some AVR chips on the ZBasic Downloads page and you can also get them from the Atmel site.
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

Thanks for your comments. The ability to translate the Zbasic code to equivalent avr-gcc code is very impressive. Is the translated C code available through the IDE?

On a separate subject, have you considered Zbasic for PIC chips? In the past few years, aside from the recession, Microchip has performed very well compared to ATMEL. Too many good companies have gone bust. I hope you and the Oak micro guy weather this storm.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:Is the translated C code available through the IDE?
See the description of the compiler option --keep-files which can be added to the .pjt file. The availability of the generated .C files is primarily of academic interest, however, since they do not include the source for ZBasic System Library calls nor do they include the original ZBasic code/comments.
liam.zbasic wrote:On a separate subject, have you considered Zbasic for PIC chips?
Several years ago when I began looking at microcontrollers again, I first studied the PIC. Frankly, I was appalled by the shortcomings of the architecture, e.g. paging, the W register bottleneck, etc. I quickly abandoned any thoughts of using the PIC for anything serious and looked for other options.

In spite of quirks of the AVR's Harvard architecture (having originally worked with 8008, 8080 and 6800 microprocessors), I find them reasonably powerful and relatively easy to use. The availablility of a fairly good open source C/C++ compiler for the AVR (avr-gcc) made the decision all the easier.
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

dkinzer wrote:The attached .zip file contains a simple project containing one ZBasic module (c_source.bas) and a C source module (factorial.c).
Hello, I tried your example "factorial" project, and the IDE echoes the following error message:

Code: Select all

>"C&#58;\Program Files\ZBasic\zbasic.exe"  --target-device=ZX40n --directory="C&#58;\$projects\zbasic2\$libraries\c_source/" --project="c_source.pjt"
Error&#58; one or more errors occurred in the back-end build process for "c_source.zxb"
>Exit code&#58; 1
I'm running IDE v1.5.6 with Compiler v3.3 on a ZX-40n device (ordered last month). I opened the example from the IDE by selecting "Project" followed by "Open...".
Are separate C source modules still supported? Thanks.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:Are separate C source modules still supported?.
Yes. The problem you're seeing arises due to the dollar signs in the path you've used. If you remove the dollar signs from all directories in the path it will work fine. Alternatively, you can add this option to your .pjt file:

Code: Select all

--use-batch-file
Background: when the ZBasic compiler builds a native mode project, it generates C source corresponding to the ZBasic code and then uses the make utility to compile and link the code, producing a downloadable file. The description language used by make recognizes a dollar sign as a special character introducing macros thus causing the pathnames present in the makefile to be modified. Using the option shown above causes a batchfile to be generated for the build process (rather than using make) and thus avoids the issue.
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

dkinzer wrote: The problem you're seeing arises due to the dollar signs in the path you've used. If you remove the dollar signs from all directories in the path it will work fine. Alternatively, you can add this option to your .pjt file:

Code: Select all

--use-batch-file
Okay, the dollar signs were the problem. I chose the "--use-batch-file" option to correct the situation. The dollar signs would've been an issue because several include statements in various projects contain them.

Questions:
* Can 1D and 2D arrays be passed to C functions?
* If so, must "option base 0" be forced in the main zbasic code?
* My preference is to NOT use projects, but to use a single program with various "include" statements and "library" statements. In this case, where would I place option statements like "--use-batch-file"?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:Can 1D and 2D arrays be passed to C functions?
Yes. However, note that C functions generally use pointer notation to specify array parameters.
liam.zbasic wrote:If so, must "option base 0" be forced in the main zbasic code?
No. In ZBasic code, you may choose any lower bound you wish. Option Base 0 and Option Base 1 select the implicit lower bound that is used when one is not specified but the lower bound may be explicitly specified, e.g.

Code: Select all

Dim myVar&#40;65 to 72&#41; as Byte
In C code the elements of this array would be referred to as myVar[0] through myVar[7]. Note, too, that the row/column order is not the same in ZBasic and C. In ZBasic, the leftmost index varies the fastest as you move from lower memory addresses to higher memory addresses while in C the rightmost index varies the fastest. In technical terms, C uses row major order while ZBasic (and VisualBasic, et al) use column major order.
liam.zbasic wrote:* My preference is to NOT use projects, but to use a single program with various "include" statements and "library" statements. In this case, where would I place option statements like --use-batch-file?
Firstly, it is important to note that C files cannot be made part of an application using #include. Secondly, Option Library is use to specify the generation of a library as opposed to the inclusion of a library in an application.

The files that comprise an application and the options used for building it appear in either the project file, in a "response file" or on the compiler invocation line. Creating a project and placing such artifacts in it .pjt file is surely the simplest method. If you choose not to use a project file you'll have to use an external means to build your application (e.g. a batch file or or other scripting method).
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

dkinzer wrote:...note that C functions generally use pointer notation to specify array parameters.
So in the Zbasic program, the C function arguments would include ByRef declarations, and the C program would include pointers, correct? Do you have a simple example that adds two matrices?

I'm working on a generalized Kalman filter, which involves several matrix operations to compute the recursive covariance matrix. I have several linear algebra functions written in C that do matrix multiplication, inversion, transpose, etc. Recoding in visual basic is foolish when I can leverage Zbasic's feature for integrating external C code.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:I have several linear algebra functions written in C that do matrix multiplication, inversion, transpose, etc.
Can you post the code (or send it to me)?
- Don Kinzer
Post Reply