ANSI C Compatibility
-
- Posts: 163
- Joined: 24 March 2008, 23:33 PM
- Location: Southern California (Blue)
ANSI C Compatibility
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.
P.
Re: ANSI C Compatibility
Yes but only with native mode devices, e.g. ZX-24n.liam.zbasic wrote:Is it possible to combine ANSI C code with Zbasic?
You can use inline C code within ZBasic source code, separate C source modules, compiled object modules and/or object libraries.
- Don Kinzer
-
- Posts: 163
- Joined: 24 March 2008, 23:33 PM
- Location: Southern California (Blue)
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.liam.zbasic wrote:Do you have a simple "hello world" example using separate C source modules?
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
Code: Select all
unsigned
factorial(unsigned val)
{
return((val <= 1) ? 1 : factorial(val - 1) * val);
}
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 3427 times
- Don Kinzer
-
- Posts: 163
- Joined: 24 March 2008, 23:33 PM
- Location: Southern California (Blue)
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.
The compiler (strictly speaking toolchain) used is GNU that includes AVR-GCC, AVR-OBJCOPY etc.liam.zbasic wrote:Questions... is the compiler related to WINAVR GCC or Elba Corp specific?
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.
There are two types of devices, ZBasic Virtual Machine (ZVM) and native.liam.zbasic wrote:Also, is there a speed advantage over all C code vs. visual basic + C? I'm considering the zx-40n device.
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
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:is the compiler related to WINAVR GCC or Elba Corp specific?
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.liam.zbasic wrote:If Elba Corp, where can I find documentation for the MCU related functions?
- Don Kinzer
-
- Posts: 163
- Joined: 24 March 2008, 23:33 PM
- Location: Southern California (Blue)
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.
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.
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:Is the translated C code available through the IDE?
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.liam.zbasic wrote:On a separate subject, have you considered Zbasic for PIC chips?
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
-
- Posts: 163
- Joined: 24 March 2008, 23:33 PM
- Location: Southern California (Blue)
Hello, I tried your example "factorial" project, and the IDE echoes the following error message:dkinzer wrote:The attached .zip file contains a simple project containing one ZBasic module (c_source.bas) and a C source module (factorial.c).
Code: Select all
>"C:\Program Files\ZBasic\zbasic.exe" --target-device=ZX40n --directory="C:\$projects\zbasic2\$libraries\c_source/" --project="c_source.pjt"
Error: one or more errors occurred in the back-end build process for "c_source.zxb"
>Exit code: 1
Are separate C source modules still supported? Thanks.
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:liam.zbasic wrote:Are separate C source modules still supported?.
Code: Select all
--use-batch-file
- Don Kinzer
-
- Posts: 163
- Joined: 24 March 2008, 23:33 PM
- Location: Southern California (Blue)
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.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
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"?
Yes. However, note that C functions generally use pointer notation to specify array parameters.liam.zbasic wrote:Can 1D and 2D arrays be passed to C functions?
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.liam.zbasic wrote:If so, must "option base 0" be forced in the main zbasic code?
Code: Select all
Dim myVar(65 to 72) as Byte
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.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?
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
-
- Posts: 163
- Joined: 24 March 2008, 23:33 PM
- Location: Southern California (Blue)
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?dkinzer wrote:...note that C functions generally use pointer notation to specify array parameters.
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.