glcc: some simple c source as play ground

Using, learning, programming and modding the Gigatron and anything related.
Forum rules
Be nice. No drama.
petersieg
Posts: 110
Joined: 28 Jun 2023, 09:06

glcc: some simple c source as play ground

Post by petersieg »

I have collected some simple c source files whenever I saw something over the years: https://github.com/petersieg/c
I use it to see what issue may arise or what can be easely compiled by other c compiles/systems. Some came from bsd-c (CP/M).
So I did this also with glcc for gigatron. Just to see what happens. Check readme.txt first. Find the zip file attached.
(I work under Mac OS - so there are ._* files in the zip - this is mac os specific and this files can be deleted on other systems)

So this is for further investigation/cleanup/correction for glcc/the source files, or what ever anyone wants to do with it.

So if you are looking for nice graphical, ready to run games => this is NOT for you ;-)

reversi (= othello), mmind (Mastermid) and startrek (needs 64k) are running fine.

Code: Select all

Generell: 
glcc complains about standard #include <stdio.h> etc. pp.
Error line numbers reported doesn't seem right (Mac OS issue?)

---

fib: fibonacci numbers - compiles - runs? - no response - heavily recursive function

hanoi - towers of .. - compiles - runs fine

mandel - mandelbrot ascii - compiles - runs but is for larger screen sizes

mmind - mastermind - compiles - runs fine

nqueens - solve n queens problem - compiles - runs? - no response

othello - compile/link issue: glink: error: undefined symbol 'kbhit' imported by 'othello.c'

reverse - compile issue: reverse.c:58: illegal statement termination -  int list[9] = {1,2,3,4,5,6,7,8,9};

reversi - othello game - compiles - runs fine - human=O, computer=X - enter x y <return>

startrek - famous startrek game - compiles - runs fine - needs 64k

c4 - connect 4 - compile issue: illegal character `\'

ttt - compile issue: ttt.c:68: syntax error; found "where you want to pl ... expecting `)'

Happy hacking.
Peter
Attachments
examples.zip
ZIP of c source code examples
(108 KiB) Downloaded 106 times
lb3361
Posts: 367
Joined: 17 Feb 2021, 23:07

Re: glcc: some simple c source as play ground

Post by lb3361 »

For heavily recursive programs, you’ll have to target a 64k map (-map=64k) because there is no space for a large stack on a 32k gigatron. This is something to suspect whenever a program compiled but does not run as it should (hangs, crashes, or worse.)

#include <stdio.h> should work. Line numbers should work. I’ll try to understand what the problem could be.

Function kbhit() is not supported. In fact, replicating <conio.h> would be a good idea, but, right now, you have to make do with <gigatron/console.h>.
lb3361
Posts: 367
Joined: 17 Feb 2021, 23:07

Re: glcc: some simple c source as play ground

Post by lb3361 »

Some findings.
  • Programs fib and queens work but take a lot of time. The recursion is not deep enough to cause problems. One can get faster results by reducing the fib NUMBER definition to 12 or reduce nqueens to n=4. Also variable 'tries' in queens is an int but should be a long because it sometimes gets printed with %ld.
  • The problems with #include arise when the files have DOS line termination (CRLF). One either needs to convert them to unix style (LF) or use the very latest GLCC version from https://github.com/lb3361/gigatron-lcc which has a patched preprocessor. This is also what causes problems in the c4 and ttt programs.
  • For function kbhit(), try

    Code: Select all

    #include <gigatron/sys.h>
    #define kbhit() (serialRaw != 0xff) 
    
  • in 'reverse' the error message arises because in C89, one cannot mix declarations and instructions. One has to move the printf instructions after the declarations.
petersieg
Posts: 110
Joined: 28 Jun 2023, 09:06

Re: glcc: some simple c source as play ground

Post by petersieg »

Thx @lb3361

I will download and give it a try the next days (busy right now).

best, Peter
petersieg
Posts: 110
Joined: 28 Jun 2023, 09:06

Re: glcc: some simple c source as play ground

Post by petersieg »

This was also in previous versions the case when compiling glcc with make under Mac OS Ventura/Homebrew:

This is triggered, because there is no glcc in path in my setup (all kept in subdirs and not installed):
../../../build/glcc -c -cpu=5 -o ../../../build/libcon1/cons_setup_5.o cons_setup.c
cpp: cons_setup.c:8 #error directive: "This library requires GLCC > 1.5-61"
make[3]: *** [../../../build/libcon1/cons_setup_5.o] Error 1

In for files cons_setup.c I have to comment out the # error directive:
#if _GLCC_VER < 104010
//# error "This library requires GLCC > 1.4-10"
#endif

But besides that, no other issue to compile glcc.

best, Peter
petersieg
Posts: 110
Joined: 28 Jun 2023, 09:06

Re: glcc: some simple c source as play ground

Post by petersieg »

c4:

Code: Select all

This glcc can not expand/understand:
  #define CHECK4(player, ix, iy, counter)\
  for (i = 1; i <= 3; i++)\
    if (!xy_pl(player, ix, iy))\
      break;
  
  CHECK4(player, ix - i, iy - i, tlbr_cnt);
  CHECK4(player, ix + i, iy + i, tlbr_cnt);
...

ttt: does now compile ;-)

reverse: Does compile after code changes:

Code: Select all

void main()
{
    int list[9] = {1,2,3,4,5,6,7,8,9};
    int tries=0;
    unsigned int i;
    int input;

    printf("Number Reversal Game. Type a number to flip the first n numbers.\n");
    printf("Win by sorting the numbers in ascending order.\n");
    printf("Anything besides numbers are ignored.\n");
    printf("\t  |1__2__3__4__5__6__7__8__9|\n");
    shuffle_list(list,9);
Again, many thanks for your help and for glcc!

best peter
lb3361
Posts: 367
Joined: 17 Feb 2021, 23:07

Re: glcc: some simple c source as play ground

Post by lb3361 »

I changed my CRLF patch to be more robust.
Now the preprocessor input routine simply converts all line terminations (CRLF or CR alone) on the fly.
This works well with c4.c, handling both the include and the multiline define.

Note that I overwrote the previous patch. You have to do

Code: Select all

$ git fetch --all ; git reset --hard origin/master
instead of git pull.
petersieg
Posts: 110
Joined: 28 Jun 2023, 09:06

Re: glcc: some simple c source as play ground

Post by petersieg »

Thx again!
Works like a charm now.
All files do compile now and run. Find updated examples*.zip attached.

Up to this point, no Gigatron specific lib/functions have been used (exception at kbhit()).
So probably much room for space reductions/improvments.

best, Peter
Attachments
examples_update.zip
(144.72 KiB) Downloaded 108 times
petersieg
Posts: 110
Joined: 28 Jun 2023, 09:06

Re: glcc: some simple c source as play ground

Post by petersieg »

Concering the possibilities to save exe space by using Gigatron specific routines:

mmind32.gt1 original without Gigatron routines: 11500 bytes
mmind32.gt1 using mincprintf, console_read/getkey: 4015 bytes :D

So app. 70% reduction!

best, Peter
Attachments
G-mmind.zip
(8.9 KiB) Downloaded 95 times
lb3361
Posts: 367
Joined: 17 Feb 2021, 23:07

Re: glcc: some simple c source as play ground

Post by lb3361 »

petersieg wrote: 21 Jul 2023, 17:48 Concering the possibilities to save exe space by using Gigatron specific routines:
mmind32.gt1 original without Gigatron routines: 11500 bytes
mmind32.gt1 using mincprintf, console_read/getkey: 4015 bytes :D
So app. 70% reduction!
best, Peter
Nice!

This is a bit of a bind. Being compatible with the ANSI C89 spec is good too.
A good way to change all the printf into mincprintf is to just to compile with option -Dprintf=mincprintf.
But if you use other stdio functions, you still pay the full price..
Post Reply