Latest on C dev for the MTX SDCC etc,

Everything about programming, including VDP and Sound programming.
under4mhz
Posts: 30
Joined: 11 Apr 2021 05:12
Location: Brisbane, Australia
Contact:

Re: Latest on C dev for the MTX SDCC etc,

Post by under4mhz »

I had the same problem. sdcc 4.2 gives about a 15% increase in fps for my games, so I think it's worth moving to. I've been submitting a bunch of bugs as I've been trying to port my code (many of which they've fixed). I haven't quite moved to 4.2 completely because of this. For a while, much of my code didn't even compile, and a few places I've had refactor to get around compiler errors it was producing. I even found a segfault. https://sourceforge.net/p/sdcc/bugs/sea ... Aunder4mhz if you're interested.

To solve the abi changes, I went through and converted my asm code to use the 4.2 conventions (ie a,hl,de) and then added wrapper code for old versions of sdcc to put the stack into the same registers. Anything I hadn't converted, I just added a __stackcall in the function declaration and it worked without changes. I'll slowly convert my asm over time as I do bug fixes on that code. I usually write my assembler as inline C functions, it's sort of all C then and I can mix C and asm (functions) as needed, so this may not help you. I tend to only write assembler if I really have to get the performance up, and then typically in low level rendering or maths functions, that needs fast looping.

sdcc 4.2 also supports undocumented instructions, so we can use ld a,iyl. Though I'd already written a macro to do this where I really needed more registers for performance (eg line rendering, 32 bit multiplies, decompressing). Unfortunately this feature has a side effect of producing a non-existent opcode in the compiler (bit 0,iyl), when there are too many if statements lined up, but I'm sure that'll get fixed soon.

Code: Select all

#if SDCCVER >= 420
#define __stackcall __sdcccall(0)
#else
#define SDCC_STACK_ABI
#define __stackcall
#endif

// Converted, works on both, but with 4.2's speed advantage
uint16_t add( uint16_t a, uint16_t b ) __naked {

    (void)a;
    (void)b;

    __asm

#ifdef SDCC_STACK_ABI
        pop     af
        pop     hl
        pop     de
        push    de
        push    hl
        push    af
#endif

        add     hl, de

#ifndef SDCC_STACK_ABI
        ex      de, hl
#endif

        ret
        
    __endasm;
        
        return 0;
}

// Not converted, still works on both, but all those stack calls.
uint16_t sub( uint16_t a, uint16_t b ) __naked __stackcall {

    (void)a;
    (void)b;

    __asm

        pop     af
        pop     hl
        pop     bc
        push    bc
        push    hl
        push    af

        sbc     hl, bc

        ret
        
    __endasm;
        
        return 0;
}

I've added the 4.2 abi calling convention table below. I found it on the MSX forum, and I keep referring back to it, so you might find it useful.

CCallConventions.png
CCallConventions.png (13.1 KiB) Viewed 5083 times
Support me on Patreon: https://www.patreon.com/Under4Mhz
Bill B
Posts: 593
Joined: 26 Jan 2014 16:31

Re: Latest on C dev for the MTX SDCC etc,

Post by Bill B »

I now have something working that attempts to satisfy Jim's request.

It uses: Install all of the above, then download and unzip the attached MTX_Projects.zip.

Instructions for using Code::Blocks to build MTX programs are in file "MTX_Projects\MTXLib\docs\codeblocks.html" in the Zip file.

I have included the beginnings of a library of C callable routines. At present, the routines are essentially equivalents of a number of the BASIC commands, so effectively it is now possible to write BASIC programs in C.

There is clearly many more routines that could be written, including disk access routines and lower level access to the MTX hardware. But I need some evidence that they will actually be used in order to make the effort worth while.

Please post any feedback or bug reports in this forum.

The existing routines are documented in "MTX_Projects\MTXLib\docs\MTXLib_Routines.html".

A quick list of all the routines:

Code: Select all

unsigned char inport (unsigned int addr);
void outport (unsigned int addr, unsigned int data);
void adjspr (unsigned char p, unsigned char n, unsigned char v);
void attr (unsigned char p, unsigned char state);
void cls (void);
void colour (unsigned char p, unsigned char n);
void crvs (unsigned char n, unsigned char t, unsigned char x, unsigned char y,
           unsigned char w, unsigned char h, unsigned char s);
void csrdown (void);
void csrhome (void);
void csrleft (void);
void csroff (void);
void csron (void);
void csrright (void);
void csrup (void);
void ctlspr (unsigned char p, unsigned char v);
void cursor (unsigned char x, unsigned char y);
void eol (void);
void genpat (unsigned char p, unsigned char n,
             unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4,
             unsigned char d5, unsigned char d6, unsigned char d7, unsigned char d8);
void ink (unsigned char clr);
char inkey (void);
void line (unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2);
void movspr (unsigned char p, unsigned char n, unsigned char d);
void pagemode (void);
void paper (unsigned char clr);
void plot (unsigned char x, unsigned char y);
void scrlmode (void);
void sprite (unsigned char n, unsigned char p, unsigned int xp, unsigned int yp,
    signed char xs, signed char ys, unsigned char c);
void view (unsigned char dir, unsigned char dis);
void vs (unsigned char n);
void killsnd (void);
void pause (unsigned int wait);
void sbuf (unsigned int nbuf);
void sound3 (unsigned int chan, unsigned int freq, unsigned int vol);
void sound7 (unsigned int chan, unsigned int freq, unsigned int vol,
	     unsigned int finc, unsigned int vinc, unsigned int durn,
	     unsigned int mode);
There is also a small program to demonstrate the compilation process.
Demo_Program.png
Demo_Program.png (9 KiB) Viewed 5071 times
Edit: Updated version of MTX_Projects.zip (revision 2) containing:
  • Revised documentation needed to support context sensitive help.
  • Bug fix for the pause() routine.
At some point I will put the MTXLib folder on GitHub. It is easier to update and the latest version can be downloaded from there at any time.
Attachments
MTX_Projects_r2.zip
(1.5 MiB) Downloaded 248 times
Last edited by Bill B on 05 Jul 2022 21:54, edited 1 time in total.
User avatar
gunrock
Posts: 245
Joined: 28 Oct 2020 21:17

Re: Latest on C dev for the MTX SDCC etc,

Post by gunrock »

Amazing work, Bill. Talk about come good on your promises! :o

I can't take a look right now as I'm away on holiday (hmm, I have my PC maybe I can sneak a look under emulation? - maybe not, the wife might not appreciate the distraction :lol:), but it looks like a promising start.
Steve G
Danish Memotech MTX 512, MFX and loving it
Potholepete
Posts: 83
Joined: 11 Aug 2012 22:13

Re: Latest on C dev for the MTX SDCC etc,

Post by Potholepete »

Fast work there Bill and impressive. I have not tried to program or use Python as yet but it looks good from the Demo above you have done and along the lines I was talking about. The more input on this from anyone who can help get an easier way for games to be written for the MTX the better in my mind.

"I have included the beginnings of a library of C callable routines. At present, the routines are essentially equivalents of a number of the BASIC commands, so effectively it is now possible to write BASIC programs in C." I won't begin to ask how you managed this as its like wizardry to me this level of work :-)

add: I have downloaded those linked files, have not installed yet as will put it onto another machine I have spare but I clicked on Memu after unzipping it and it opens up almost full screen with the cursor waiting for my command but I cannot see (probably right in from of my eyes lol) how to run your demo from with Menu?
Bill B
Posts: 593
Joined: 26 Jan 2014 16:31

Re: Latest on C dev for the MTX SDCC etc,

Post by Bill B »

Potholepete wrote: 04 Jul 2022 16:18 I have not tried to program or use Python as yet but it looks good
Python is only used to convert the Intel Hex files produced by SDCC into MTX RUN files. I could instead have written a small program in C to do that, but then I would have to maintain separate versions for Windows and Linux. Using Python, the same program will run Windows, Linux, Mac, other...
Potholepete wrote: 04 Jul 2022 16:18 "I have included the beginnings of a library of C callable routines. At present, the routines are essentially equivalents of a number of the BASIC commands, so effectively it is now possible to write BASIC programs in C." I won't begin to ask how you managed this as its like wizardry to me this level of work :-)
What made this easier was that (if you look at the MTX manual), most of the display routines can be performed by sending control or escape sequences.
User avatar
Dave
Posts: 1280
Joined: 11 Aug 2012 18:16
Contact:

Re: Latest on C dev for the MTX SDCC etc,

Post by Dave »

Just for info . . . it may help others . . . .

My AV program (Norton 360) "helpfully" deleted memu-win.exe and portaudio_x64.dll from the MEMU run time directory (on the basis that the files were "little known")

If Bill's test file does not auto run after compilation, you might want to check that the memu files are actually there
Bill B
Posts: 593
Joined: 26 Jan 2014 16:31

Re: Latest on C dev for the MTX SDCC etc,

Post by Bill B »

On the subject of MEMU, during this development, I discovered a small issue.

MEMU has a feature where you can specify a *,RUN file on the end of the command line, and MEMU will automatically load and run the file, without having to type USER RUN "FILENAME.RUN". I use this to automatically run programs at the end of a Code::Blocks compilation.

What I found was that the code in MEMU disables interrupts before automatically loading the RUN file. I don't know why Andy did this, because the USER RUN command does not disable interrupts. A comment in the code suggests that he found at least one game that had a problem with interrupts being enabled.

It is significant because the sprites in my demo program rely on interrupts being enabled. They won't even appear on the screen if there aren't any interrupts. It took me a ridiculous amount of time to work this out.

My version of MEMU, which is included in the MTX_Projects.zip file no longer disables interrupts when loading a RUN file from the command line unless you also specify the new option -run-no-interrupts.

As a consequence of this, if you use Andy's version of MEMU rather than mine, you will have to test the demo program using USER RUN "DEMO.RUN", rather than just specifying demo.run on the command line.
Bill B
Posts: 593
Joined: 26 Jan 2014 16:31

Re: Latest on C dev for the MTX SDCC etc,

Post by Bill B »

Dave pointed out that Code::Blocks is capable of displaying context sensitive help. Thanks Dave.

I have now provided context sensitive help for the MTXLib routines. To enable this:
  • Replace the contents of "MTX_Projects\MTXLib\docs" with the contents of the attached "docs.zip".
  • Open Code::Blocks and select "Settings / Environment..." from the menu.
  • On the resulting dialog, scroll down to the bottom of the left hand pane and select "Help files".
  • Click on the "Add" button on the right hand side of the dialog.
  • Set the help title to "MTXLib" and click "OK".
  • Select "Yes" to browse for the help file location.
  • In the file open dialog, select "All files (*.*)", then browse to MTX_Projects\MTXLib\docs\routines\MTXLib.html. Click "Open" to select the file.
  • You should now have the Help settings dialog, with the full path to the selected MTXLib.html" file (see image).
  • Edit this path, replacing "MTXLib.html" by "$(keyword).html".
  • Check the "This is the default help file" box.
  • Enter "MTXLib" in the "Default keyword value" edit box.
  • Click "OK" to close the dialog.
Help_Configuration.png
Help_Configuration.png (42.63 KiB) Viewed 5017 times
Now, if you open a C source file, position the cursor on the name of an MTXLib routine and type the <F1> key, you should get an HTML page describing the parameters of the selected routine.

For some reason this only appears to be available on the Windows version of Code::Blocks. It is not available on the version installed by the package manager on my Ubuntu laptop.
Attachments
docs.zip
(384.75 KiB) Downloaded 252 times
Bill B
Posts: 593
Joined: 26 Jan 2014 16:31

Re: Latest on C dev for the MTX SDCC etc,

Post by Bill B »

Kudos to Dave for finding the first bug :oops:

The count down in the pause routine was broken. New version attached.

I will also update the MTX_Projects zip file in the earlier post.
Attachments
pause.zip
(466 Bytes) Downloaded 247 times
Bill B
Posts: 593
Joined: 26 Jan 2014 16:31

Re: Latest on C dev for the MTX SDCC etc,

Post by Bill B »

More interrupt fun and games. It turns out that the way MEMU emulates the Z80 HALT instruction was breaking the way the execution speed is controlled. :(

Although MEMU normally attempts to run at a realistic speed, it only does so on average. Individual Z80 instructions do not take the correct amount of time. Instead it periodically checks how many machine cycles have been executed and then inserts a delay to bring the total time up to what would be expected for that number of cycles.

Repeatedly executing HALT, as the pause() routine does, was not correctly counting the number of cycles executed, resulting in the program running much faster than expected.

The attached Zip file contains versions of MEMU that correct this issue. The timing will never be as exact as on a real MTX, but should now be closer.
Attachments
memu.zip
(514.76 KiB) Downloaded 254 times
Post Reply