Welcome, Guest. Please login or register.


Author Topic: Enterprise C compiler for PC (Read 33406 times)

Offline MrPrise

  • Administrator
  • EP addict
  • *
  • Posts: 2764
  • Country: hu
    • Enterprise Forever
Enterprise C compiler for PC
« on: 2007.March.09. 00:53:39 »
Gep would like to know here if there is any C compiler for the Enterprise which runs on PC? He would like to tinker with the EP, but he would also like to enjoy the PC's comfort. After searching on SF he found the SDCC which looks promising.

Offline Tuby128

  • EP addict
  • *
  • Posts: 1482
  • Country: hu
Re: Enterprise C compiler for PC
« Reply #1 on: 2015.May.03. 23:20:15 »
Hello,

 I found this topic very close to my today/yesterday work. I know, the last post came in 2007, but who knows, maybe we can help each other.

The reason why I started to seek for a Z80 C-Compiler, because I cannot program big things in Assembly. And I got used to use C for programming.

 So I have started yesterday with SDCC, Today I made a little program, and tested it. I wanted to share my experiences.

For a new Project I do the following:

1. I create a C-code with the 'int main(void)' function and other sub functions
2. Compile the project with the folowing command which places the file at memory 0x4000
   sdcc main.c -mz80 --code-loc 0x4000 --data-loc 0x4000
3. Then I convert the created main.ihx file to binary format using the hex2bin.exe
  hex2bin.exe -p 0 main.ihx
 (-p 0 means, the unused bytes im file has to be ZERO)
4. I copy the data from 0x4000 to the end and I give it to the emulator

 As I started the program from 0x4000, it didn't work, the program returned immediately. As I made a dissassembly listing I had noticed, that the first 10 bytes are foreign code. See attachment, the red line shows where the first planned instruction starts, the instruction before 'DEC SP' means the main function has a integer return value it's still ok, but the others are not from me.
 If I jump it over, and call 0x400A, the program runs normally.

 Does anyone know, what for are these first 10 bytes, and how can I switch it off in SDCC?

Offline lgb

  • EP addict
  • *
  • Posts: 3563
  • Country: hu
  • æðsta yfirmaður
    • http://lgb.hu/
Re: Enterprise C compiler for PC
« Reply #2 on: 2015.May.03. 23:35:57 »
Dunno, but some time ago I played with sdcc for EP: http://ep.lgb.hu/sdcc/ Please note, that sdcc seems to be a bit odd to change internals all the time. For example it had an odd behaviour to store data as Z80 code to store the needed code by executing Z80 ops actually instead of just store the data bytes. I had some solution to try to "convert" this into a sane form, but it seems it's depend on the sdcc version. And similar things. I've also made a bug report to the sdcc team about the issue (sdcc should store data as data and not as code which initializes the data ...) I have no idea about the current state of my bug report, or this issue though ...

You can try to follow my Makefile if it helps at all. My intent was creating "normal" EXOS-5 header program right from C source compiled with sdcc, so with some major further development, at least simple C programs can be compiled for the Enterprise without the need to load them to specific address etc, just LOAD them, and that's all.

Also, you can visit this Hungarian topic (about z88dk, but also sdcc related): http://enterpriseforever.com/programozas/z88dk/
« Last Edit: 2015.May.03. 23:47:36 by lgb »

Offline Tuby128

  • EP addict
  • *
  • Posts: 1482
  • Country: hu
Re: Enterprise C compiler for PC
« Reply #3 on: 2015.May.03. 23:42:46 »
I found the following list in the .map file:

Code: [Select]
     Value  Global                              Global Defined In Module
      -----  --------------------------------   ------------------------
     00004000  __clock                            crt0
     00004004  _exit                              crt0
     0000400A  _main                              main02
     0000400A  _main_start                        main02
     00004062  _main_end                          main02

According to this list, it has to be a clock and exit functions, which belong to crt0.
I haven't made crt0 module.
« Last Edit: 2015.May.03. 23:46:15 by Tuby128 »

Offline Tuby128

  • EP addict
  • *
  • Posts: 1482
  • Country: hu
Re: Enterprise C compiler for PC
« Reply #4 on: 2015.May.04. 00:01:28 »
I found the solution here, I searched for 'CRT0'
http://sdcc.sourceforge.net/mediawiki/index.php/Z80_port

To get rid of the problem I have to use the '--no-std-crt0' option. And it works perfectly.

So my second point is changed:
2. Compile the project with the folowing command which places the file at memory 0x4000
   sdcc main.c -mz80 --no-std-crt0 --code-loc 0x4000 --data-loc 0x4000

Offline Tuby128

  • EP addict
  • *
  • Posts: 1482
  • Country: hu
Re: Enterprise C compiler for PC
« Reply #5 on: 2015.May.04. 00:07:17 »
LGB, can you show a short example how can I append Inline assembly code?
 I am afraid, that the inline ASM code changes a register, and it generates an error.

Offline lgb

  • EP addict
  • *
  • Posts: 3563
  • Country: hu
  • æðsta yfirmaður
    • http://lgb.hu/
Re: Enterprise C compiler for PC
« Reply #6 on: 2015.May.04. 00:29:11 »
To get rid of the problem I have to use the '--no-std-crt0' option. And it works perfectly.

Yeah. I told to look at my Makefile at the URL I've already posted, it may help, unfortunately I don't remember these things now :(

Offline lgb

  • EP addict
  • *
  • Posts: 3563
  • Country: hu
  • æðsta yfirmaður
    • http://lgb.hu/
Re: Enterprise C compiler for PC
« Reply #7 on: 2015.May.04. 00:38:44 »
LGB, can you show a short example how can I append Inline assembly code?
 I am afraid, that the inline ASM code changes a register, and it generates an error.

Sorry, but I have no idea. What I've done is to starting to create an "Enterprise library" for sdcc (including crt0, yes) but those were separated asm files, and just the linking stage was where they were "combined" to an Enterprise EXOS-5 program. I've never tried to insert inline assembly into a C source with sdcc, if your question was about that.

Sdcc manual at http://sdcc.sourceforge.net/doc/sdccman.pdf may help you though. It has some hints about the "naked" functions (= a function written in assembly fully) and what registers can be used or not (also, for a full assembly but C callable example can be seen in my sources in crt0.s written by me, but it's not "inline" assembly). I have really no idea about inline assembly fragments really inserted into a C function with also C code ...

Offline Tuby128

  • EP addict
  • *
  • Posts: 1482
  • Country: hu
Re: Enterprise C compiler for PC
« Reply #8 on: 2015.May.04. 02:13:38 »
I am through the user manual of SDCC, and some search in diverse websites.
The conclusion is, SDCC lacks of variable-passing between C-code and inline assembly code. And the support appears dead.

It would be very important using a variable bound with a CPU-register, to accelerate the execution time.

 I will shut down the study of SDCC, and I will try other programs like the Z88DK.

Offline lgb

  • EP addict
  • *
  • Posts: 3563
  • Country: hu
  • æðsta yfirmaður
    • http://lgb.hu/
Re: Enterprise C compiler for PC
« Reply #9 on: 2015.May.04. 07:13:50 »
I am through the user manual of SDCC, and some search in diverse websites.
The conclusion is, SDCC lacks of variable-passing between C-code and inline assembly code. And the support appears dead.

It would be very important using a variable bound with a CPU-register, to accelerate the execution time.

Why is it strange for you? This is natural for C, there are no "registers" in C too much, just variables, bound to memory locations (thus subject of pointers etc), this is natural. C compilers usually have the complexity to keep some variables in the registers (traditionally there was the "register" keyword to organize this, but a modern C compiler will surely make better code if you don't try to outsmart it, if it handles for that keyword at all and not just silently ignores). Often (always?), C compilers are two pass compilers ie, generating assembly code first than doing the assembly. Some compilers even runs optimizations through the generated asm code. The problem, that in-line assembly in the that stage can be an issue, as that kind of optimizator only see the asm code regardless written by you or generated from the C code, mixed together within even the same function. Etc, etc. So I am not surprised not having too much support in C compilers for that. Why would you need it? OK, "to accelerate the execution time" but then one can say do not use C at all, but only asm to accelerate it further :) Using C is more about accepting the basic construction of the language which is memory/stack oriented (stack based ... no like forth but the implementation, it is transparent to the programmer usually) and not register based.

Z88DK is actually not a C compiler but Small-C (well it seems, its origin is about Small-C and nowdays it supports maby ANSI C stuffs, I can't judge too much ...), so some features of the C programming language is not supported. Also please note, that the problems with "direct control on registers" may seem a real issue for you, it's also one of the key  factors to write an efficient C compiler. So a C compiler which gives you more control on things you would like would be (probably) slower, ie generating worse code from the same C source.
« Last Edit: 2015.May.04. 09:14:56 by lgb »

Offline Tuby128

  • EP addict
  • *
  • Posts: 1482
  • Country: hu
Re: Enterprise C compiler for PC
« Reply #10 on: 2015.May.04. 10:08:01 »
I want to something like that (extract from website http://wiki.osdev.org/Inline_Assembly)

Code: [Select]
int a=10;
int b;

asm ("
      movl %1, %%eax;    // eax <- a
     ....
     (doing other things here)
     ....
      movl %%eax, %0;"  //  b <- eax
     :"=r"(b)        /* output  , %0*/
     :"r"(a)         /* input , %1 */
     :"%eax"         /* clobbered register */
     );

With this snippet I can pass variable (int a) thru register (eax) to a ASM code, and at the end, I can pass an another back (int b).

Offline lgb

  • EP addict
  • *
  • Posts: 3563
  • Country: hu
  • æðsta yfirmaður
    • http://lgb.hu/
Re: Enterprise C compiler for PC
« Reply #11 on: 2015.May.04. 10:24:21 »
Ah yes, the "gcc in-line assembly black magic with at&t syntax" as it is referred by some funny people :) The complexity of this syntax is exactly because of the fact that the compiler should "integrate" your asm code as a part of the compiled C code. I guess, at least. So it's kinda hard to provide this feature in an efficient way, and probably "low end compilers" (ok, sdcc may not means to have a compiler "as serious" as gcc is) can't provide this flexibility because of its complexity. But again, this is my best guess only ... In case of sdcc I would go for naked/assembly functions so you write a whole function in assembly when it's easier to tell how you control the usage of registers, etc because context is not "mixed" with your own asm code and the compiled C code within the same function. Of course this is not the best solution if you need some short asm code only when you will have the overhead to call a function all the time when you need to invoke your code. But the overhead is minimal if your "all asm" function is heavy enough anyway and not just a few opcodes.

Offline Tuby128

  • EP addict
  • *
  • Posts: 1482
  • Country: hu
Re: Enterprise C compiler for PC
« Reply #12 on: 2015.May.04. 16:34:25 »
1. Thing which came to my mind
Could show you an exaple as well, why we need inline Assembly:
 If we want to fill memory incrementally, e.g. from 0x1000 to 0x3000 with the same value (0x01), we do the following

This is a for loop made by SDCC
Code: [Select]
label1:
- HL <= 1000
- (HL) <= 0x01
- HL++
- IF HL= 3000
     RETURN
- GOTO label1

option 2, an user made inline asm:

Code: [Select]
label1:
- SP <= 3000
- PUSH 0x01   - This instruction seems to be faster on EP
-  IF SP=1000 THEN
     RETURN
- GOTO label1

2. Thing
 A normal call saves all register into memory, plus uses the stack, and call a block. The inline call means, the compiler paste the code everywhere, where it's called, directly. It produces more code, but faster execution.

Offline Tuby128

  • EP addict
  • *
  • Posts: 1482
  • Country: hu
Re: Enterprise C compiler for PC
« Reply #13 on: 2015.May.04. 22:29:31 »
I found the following website where C compiler is developed, and someone shared how to use the utils.
https://github.com/earl1k/llvm-z80/issues/2
See comment:  Apr 25, 2014

He mentioned LLVM and CLANG.

A site about LLVM:
https://olduino.wordpress.com/2014/12/30/llvm-for-the-z80-another-source-of-inspiration/

Offline lgb

  • EP addict
  • *
  • Posts: 3563
  • Country: hu
  • æðsta yfirmaður
    • http://lgb.hu/
Re: Enterprise C compiler for PC
« Reply #14 on: 2015.May.04. 22:33:26 »
I found the following website where C compiler is developed, and someone shared how to use the utils.
https://github.com/earl1k/llvm-z80/issues/2
See comment:  Apr 25, 2014

He mentioned LLVM and CLANG.

A site about LLVM:
https://olduino.wordpress.com/2014/12/30/llvm-for-the-z80-another-source-of-inspiration/

Yes, LLVM is a young but serious stuff. I mean even for "mainstream CPUs", head-to-head with gcc now, as I can feel. The interesting thing about LLVM is the easy adoption for new CPUs, which is in contrast (?) of gcc. But please note I am far from being an expert of C compiler internals, so maybe it's just a feeling I can express this way. I've read various "mad" targets of LLVM including Z80 and 6502 too, but I am not sure how serious (never checked) projects they are, are they usable at all, etc ...