68k-mbc C Programming Example USERLED

by coopzone in Circuits > Arduino

790 Views, 1 Favorites, 0 Comments

68k-mbc C Programming Example USERLED

Untitled.png

I am going to use the example of flashing the User LED on the 68k-mbc as an example of producing a working C program. This will evolve many of the standard tasks for compiling a C program. And we will also create to external functions peek and poke that act the same as the BASIC functions in the original userled.bas.

The main steps:

Using Ed to create a simple file. Not a tutorial you will need to get familiar with it yourself.

Create an assembler program. And compiling it.

Create a C program. And compiling to an executable file.

Linking all the modules together.

Hopefully this guide will give you enough to get started with C.

Supplies

Before you can compile this C program you will need a working 68k-setup , I assume you have booted from the standard SD-Card and that will have the C compiler installed on Drive C:

You should also be familiar with ED, It seems to be the only editor on cp/m-68k at present.

Checking Your C Install

Just as a quick check do a DIR on Drive C: you should get:

C>dir
C: AR68     68K : AS68     68K : C068     68K : C168     68K : LINK68   68K 
C: CP68     68K : LO68     68K : NM68     68K : SENDC68  68K : SIZE68   68K 
C: AS68INIT     : C        SUB : CLINK    SUB : CE       SUB : MORE     C   
C: SETJMP   H   : MORE     O   : README   TXT : XFLDBIOS H   : C        OUT 
C: CLIB         : AS68SYMB DAT : CLINKF   SUB : CLINKE   SUB : S        O   
C: BDOS     S   : LIBE     A   : LIBF     A   : CPMLIB       : PP       O   
C: ASSERT   H   : BIOSTYPS H   : CTYPE    H   : ERRNO    H   : LOADBIOS H   
C: NORMBIOS H   : OPTION   H   : OSATTR   H   : OSIF     H   : OSIFERR  H   
C: PORTAB   H   : SIGNAL   H   : STDIO    H   : XFNMBIOS H   : XLOADBIO H   
C: XNORMBIO H   : BDOS     O

Your directory will probably look a little different, I have already created some extra files. But you should have the .68k files and the .sub files.

By default the C installation has an example program called "more", it's similar to the unix more command. The source file is "more.c", do the following steps to compile it. This will produce a more.68k file that can be used to page files to the console.

You only need type "c more", the rest is example output.

C>c more

C>CP68 -I 0: MORE.C MORE.I
more.c, # line 2: (warning) EOF redefined

C>C068 MORE.I MORE.1 MORE.2 MORE.3 -F
"more.c", * 12: (warning) integral type expected

C>ERA MORE.I

C>C168 MORE.1 MORE.2 MORE.S

C>ERA MORE.1

C>ERA MORE.2

C>AS68 -L -U -S 0: MORE.S

C>ERA MORE.S

Next you link the program output more.o and any C libraries required by it, like this:

Again you only need to type the first line "clink more bdos"

C>clink more bdos

C>LO68 -R -U_NOFLOAT -O MORE.68K 0:S.O MORE.O BDOS.O .O .O .O .O .O .O .O 0:CLIB

C>

And you now have more.68k, the two submit files you used "c" and "clink" are shortcuts to the compiler and linker. They both include information that you would soon get bored of typing!. You can use "more" to look at them "more c.sub" or just the CP/M "type c.sub".

Ready to go..

Create the Peek and Poke External Modules

Before I start - I know there may well be other ways to do this. But I wanted have a go at linking C programs to assembler and maybe even Pascal modules. That way if I write libraries I have a wider choice.

First create a file on drive C:, You can use ed and cut/paste the contents from below. But be aware without hardware flow control cut/paste needs to be don in small amounts. The command(s) are:

C>
C>A:ED pp.s
(once ed has created the file)
*I
(paste in)
^Z
*E

The ^Z means type CTRL-Z to terminate input mode. You may need to take some time and practice with ED. It's ok as a simple editor and since it's the only one on CP/M-68k you don't get a lot of choice! The assembler Code:

* PEEK and Poke for C
*
.globl _poke
.globl _peek
.text
*
_peek:
        move.l 4(a7),a1
        moveq #0,d0
        move.b (a1),d0
        rts

_poke:
        move.l 4(a7),a1
        move.w 8(a7),d0
        move.b d0,(a1)
        rts
.end

Again apologies to the 68000 programmers, this is my first ever 68000 code and i'm just glad it did what I thought it would, eventually!

The only points to note are the _ (underscores) at the beginning of the function names. That's because C adds them automatically for external functions, so the linker looks for them when trying to resolve names.

To create an object file from this code do the following:

as68 -l -u -s 0: pp.s<br>

You should get no error output, if you do it's back to ED to try and correct your source!

The output produces a file called pp.o Thats "o" for object file, a simple linkable file containing two external names peek and poke (actually _peek and _poke)

Next the C program

The C Program

It's time to create the C program source code. Just as we did with the peek and poke source code, you use ED and cut and paste the following program. Remember cut/paste need to be done is small sections.

You type:

c>
c>A:ED userled.c
(once create paste in the code)
*i
(pste in)
^Z
*e

The code to paste is:

#include <stdio.h>

#define CPORT 1048573
#define DPORT 1048572
#define TIME 4000

void ledst(l)  /* l=0 led off, l=1 led on */
int l;
{
        poke(CPORT,0); 
        poke(DPORT,l);
}

int userkey() /* Return the user key value 0=not pressed 1=pressed */
{
        poke(CPORT,128);
        return( peek(DPORT) );
}

void main()

{       
int i,LEDstate;
        
printf("User LED now flashing, press USR key to exit\n");
        
while(1) /* Repeat this while loop for ever */
{       
for(LEDstate = 0; LEDstate < 2; LEDstate++) /* LEDstate counts from 0 to 1, ie LED is on/off */
  {     
  ledst(LEDstate);
  for(i = 0; i < TIME; i++)
     {  
       if( userkey() )
       {
         printf("USR key pressed, exiting...\n");
         ledst(0); /* clean up turn off LED */
         exit(0);
       }
     }  
  }     
}  /* end of while loop */
}       

The program consists of two functions "ledst" and "userkey" then the main program. The two functions interface with the peek and poke routines from the assembler code.

The main program, is essentially three loops, outside loop is a while foreever do this... Then inside that is a for loop that counts from 0 to 1 (variable LEDstate). In other words led off led on led pff etc etc. The inside loop acts as a delay and counts using another for loop from 0 to TIME (set to 4000). Inside this loop is a test using the userkey function that exits the program if the key is pressed.

Next compile the program...

To Compile Your C Program

Once you have all the code type in, you use the "c.sub" to compile it to an object file, same as we did with the "more" command. You type: (just the first line)

C>c userled

C>CP68 -I 0: USERLED.C USERLED.I

C>C068 USERLED.I USERLED.1 USERLED.2 USERLED.3 -F

C>ERA USERLED.I

C>C168 USERLED.1 USERLED.2 USERLED.S

C>ERA USERLED.1

C>ERA USERLED.2

C>AS68 -L -U -S 0: USERLED.S

C>ERA USERLED.S

The end result is a file called userled.o. The "c" sub does a lot of cleaning up, removing temp files etc. You can run the commands one at a time if you like and examine the create files at each step.

Make the final program next, by linking.

Using the Linker

You now have two files pp.o and userled.o These are the object files we will link together in this last stage to produce a .68k file to use.

This is the command to use:

C>clink userled pp

C>LO68 -R -U_NOFLOAT -O USERLED.68K 0:S.O USERLED.O PP.O .O .O .O .O .O .O .O 0:CLIB

C>

the "clink" sub file take most of the hard work out of it by including the s.o and CLIB libraries for us automatically. Our line just includes the userled file and the pp file. Note the sub file adds the .o to them for us.

The end result is a file called userled.68k, you can run this and see the results.

Have fun