Close

Built demo.c

A project log for Retro 68000 CPU in an FPGA

Making a Retrocomputer with a 68000 CPU in an FPGA

land-boardscomland-boards.com 08/30/2020 at 14:120 Comments

Jeff Tranter has a piece of demo code that is written in C.  The code prints lines with number of 1 to 11 along with the number squared, the number to the 4th power and the factorial of the number

Makefile Issues

Jeff's Makefile has troubles with my version of the GCC compiler but all I needed to do to fix it was remove the include for the coff file. Doesn't seem to be a problem since obj-copy figures out the file type without it. I had the same issue with building assembly code.

The Makefile has:

all:    demo.run demo.s

demo.run: demo.c
    /opt/m68k-elf/bin/m68k-elf-gcc -Wall -m68000 -msoft-float -c demo.c
    /opt/m68k-elf/bin/m68k-elf-ld --defsym=_start=main -Ttext=0x2000 -Tdata=0x3000 -Tbss=0x4000 --section-start=.rodata=0x5000 demo.o `/opt/m68k-elf/bin/m68k-elf-gcc -m68000 -print-libgcc-file-name`
    /opt/m68k-elf/bin/m68k-elf-objcopy -O srec a.out demo.run

demo.s: demo.c
    /opt/m68k-elf/bin/m68k-elf-gcc -Wall -nostdlib -nodefaultlibs -m68000 -S demo.c

clean:
    $(RM) a.out demo.o demo.run demo.s    

Running the Program

The program starts running at address 0x2000 and does the following:

TUTOR  1.3 > GO 2000
PHYSICAL ADDRESS=00002000
Start
n  n^2  n^4  n!
1 1 1 1
2 4 8 2
3 9 27 6
4 16 64 24
5 25 125 120
6 36 216 720
7 49 343 5040
8 64 512 40320
9 81 729 362880
10 100 1000 3628800
11 121 1331 39916800
Done

TUTOR  1.3 >

Useful Functions in example code

Jeff provides two functions which use the TUTOR monitor for calls:

void outch(const char c);
void printString(const char *s);

The code for these programs is: 

// Print a character using the TUTOR monitor trap function.
void outch(char c) {
    asm("movem.l %d0/%d1/%a0,-(%sp)\n\t"  // Save modified registers
        "move.b %d0,%d0\n\t"              // Put character in D0
        "move.b #248,%d7\n\t"             // OUTCH trap function code
        "trap #14\n\t"                    // Call TUTOR function
        "movem.l (%sp)+,%d0/%d1/%a0");    // Restore registers
}

// Print a string.
void printString(const char *s) {
    while (*s != 0) {
        outch(*s);
        s++;
    }
}

The outch( ) routine is conveniently written in assembly so it provides a nice example of in-line assembly language code. 

Return to TUTOR

main( ) cleanly returns to the Tutor monitor. It does that by using a TRAP:

// Go to the TUTOR monitor using trap 14 function. Does not return.
void tutor() {
    asm("move.b #228,%d7\n\t"
        "trap #14");
}

The trap 14 handler code in TUTOR header has:

8162                   *-------------------------------------------------------------------------
 8163                   * File TRAP14    Trap 14 handler of "TUTOR"                       06/25/82
 8164                   
 8165                   *        CALLING SEQUENCE
 8166                   *                  %D7 = XXXXXXFF   WHERE "FF" IF FUNCTION NUMBER
 8167                   *                  TRAP      #14
 8168                   
 8169                   TRAP14:
 8170 be70 48E7 4160              MOVEM.L %D1/%D7/%A1-%A2,-(%A7)

This finally validates the GCC toolchain works.

Discussions