running compiled programs?

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
ThePerfectK
Insane DCEmu
Insane DCEmu
Posts: 147
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Thu Apr 27, 2006 10:15 am
Has thanked: 27 times
Been thanked: 35 times

running compiled programs?

Post by ThePerfectK »

I've got my toolchain all set up and successfully compile projects. I'm going through and building some examples included with KOS (hello world, png, etc) and I can get an elf built. Problem is, I don't know how to get that elf running on my dreamcast.

Here is the Makefile for the example called "png":

Code: Select all

all: rm-elf example.elf

include $(KOS_BASE)/Makefile.rules

OBJS = example.o wfont.o

KOS_LOCAL_CFLAGS = -I$(KOS_BASE)/addons/zlib
	
clean:
	-rm -f example.elf $(OBJS)
	-rm -f romdisk_boot.*

dist:
	-rm -f $(OBJS)
	-rm -f romdisk_boot.*
	$(KOS_STRIP) example.elf
	
rm-elf:
	-rm -f example.elf
	-rm -f romdisk_boot.*

example.elf: $(OBJS) romdisk_boot.o 
	$(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $@ $(KOS_START) $^ -lpng -lz -lm $(KOS_LIBS)

wfont.o: wfont.bin
	$(KOS_BASE)/utils/bin2o/bin2o $< wfont $@

romdisk_boot.img:
	$(KOS_GENROMFS) -f $@ -d romdisk_boot -v

romdisk_boot.o: romdisk_boot.img
	$(KOS_BASE)/utils/bin2o/bin2o $< romdisk_boot $@

run: example.elf
	$(KOS_LOADER) $<
this gives me a file called example.elf (along with romdisk.img and romdisk.o) when I run make. I've tried converting the elf into a bin using:

Code: Select all

sh-elf-objcopy -R .stack -O binary example.elf example.bin
I compiled marcus' bin scrambler and I'm scrambling my bin like so:

Code: Select all

scrambler example.bin 1ST_READ.bin
this gives me a file called 1ST_READ.bin. I then build an ISO using mkisofs providing the IP.BIN from a download of nestopia I grabbed off of dcemulation's front page:

Code: Select all

mkisofs -V TEST_ISO -G IP.BIN -joliet -rock -l -o test.iso /usr/local/dc/binscrambler/1ST_READ.BIN
this gives me test.iso. I then take test.iso and use cdi4dc to convert the iso into a cdi file:

Code: Select all

cdi4dc.exe test.iso test.cdi
finally, I burn test.cdi using a copy of imgburn that works with cdi files. The burn completes like normal, I pop the disc into my dreamcast and... nothing. It comes up as an audio disc sometimes, othertimes it'll just boot me back to the dreamcast menu with play highlighted (and pressing play will go through the boot sequence then dump me back to the dreamcast menu again). If I try using the utopia boot CD first, the program never runs (it just stays on the reindeer screen).

People have suggested using dreamshell and an SDcard reader - I have both. I don't know if the SDcard reader is working - the light never turns on, and the SDcard itself isn't recognized in dreamshell. If I go to ISO loader in dreamshell, the SD card section is greyed out. If I go to file explorer, I get this weird error where the resolution of the file explorer is too much for my TV (I'm using a standard CRT tv, with the dreamcast connected via s-video). Booting up file explorer will give me a half-second glimpse of the menu, then my TV will report unusable signal. I've ordered a VGA cable to try it out on a normal monitor, maybe that'll help.

I haven't tried loading the elf in nulldc -- that's an option down the line, but first I want to get a project, any project running on my actual dreamcast. Any ideas?

EDIT: Best include the source for the png example, just in case:

Code: Select all

/* png example for KOS 1.1.x
 * Jeffrey McBeth / Morphogenesis
 * <mcbeth@morphogenesis.2y.net>
 *
 * Heavily borrowed from from 2-D example
 * AndrewK / Napalm 2001
 * <andrewk@napalm-x.com>
 */

#include <kos.h>
#include <png/png.h>
#include <zlib/zlib.h>

/* font data */
extern char wfont[];

/* textures */
pvr_ptr_t font_tex;
pvr_ptr_t back_tex;
char *data;

/* init background */
void back_init() {
    back_tex = pvr_mem_malloc(512 * 512 * 2);
    png_to_texture("/rd/background.png", back_tex, PNG_NO_ALPHA);
}

/* init font */
void font_init() {
    int i, x, y, c;
    unsigned short * temp_tex;

    font_tex = pvr_mem_malloc(256 * 256 * 2);
    temp_tex = (unsigned short *)malloc(256 * 128 * 2);

    c = 0;

    for(y = 0; y < 128 ; y += 16)
        for(x = 0; x < 256 ; x += 8) {
            for(i = 0; i < 16; i++) {
                temp_tex[x + (y + i) * 256 + 0] = 0xffff * ((wfont[c + i] & 0x80) >> 7);
                temp_tex[x + (y + i) * 256 + 1] = 0xffff * ((wfont[c + i] & 0x40) >> 6);
                temp_tex[x + (y + i) * 256 + 2] = 0xffff * ((wfont[c + i] & 0x20) >> 5);
                temp_tex[x + (y + i) * 256 + 3] = 0xffff * ((wfont[c + i] & 0x10) >> 4);
                temp_tex[x + (y + i) * 256 + 4] = 0xffff * ((wfont[c + i] & 0x08) >> 3);
                temp_tex[x + (y + i) * 256 + 5] = 0xffff * ((wfont[c + i] & 0x04) >> 2);
                temp_tex[x + (y + i) * 256 + 6] = 0xffff * ((wfont[c + i] & 0x02) >> 1);
                temp_tex[x + (y + i) * 256 + 7] = 0xffff * (wfont[c + i] & 0x01);
            }

            c += 16;
        }

    pvr_txr_load_ex(temp_tex, font_tex, 256, 256, PVR_TXRLOAD_16BPP);
}

void text_init() {
    int length = zlib_getlength("/rd/text.gz");
    gzFile f;

    data = (char *)malloc(length + 1); // I am not currently freeing it

    f = gzopen("/rd/text.gz", "r");
    gzread(f, data, length);
    data[length] = 0;
    gzclose(f);

    printf("length [%d]\n", length);
}

/* draw background */
void draw_back(void) {
    pvr_poly_cxt_t cxt;
    pvr_poly_hdr_t hdr;
    pvr_vertex_t vert;

    pvr_poly_cxt_txr(&cxt, PVR_LIST_OP_POLY, PVR_TXRFMT_RGB565, 512, 512, back_tex, PVR_FILTER_BILINEAR);
    pvr_poly_compile(&hdr, &cxt);
    pvr_prim(&hdr, sizeof(hdr));

    vert.argb = PVR_PACK_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
    vert.oargb = 0;
    vert.flags = PVR_CMD_VERTEX;

    vert.x = 1;
    vert.y = 1;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 640;
    vert.y = 1;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 1;
    vert.y = 480;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 1.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 640;
    vert.y = 480;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 1.0;
    vert.flags = PVR_CMD_VERTEX_EOL;
    pvr_prim(&vert, sizeof(vert));
}

/* draw one character */
void draw_char(float x1, float y1, float z1, float a, float r, float g, float b, int c, float xs, float ys) {
    pvr_vertex_t    vert;
    int             ix, iy;
    float           u1, v1, u2, v2;

    ix = (c % 32) * 8;
    iy = (c / 32) * 16;
    u1 = (ix + 0.5f) * 1.0f / 256.0f;
    v1 = (iy + 0.5f) * 1.0f / 256.0f;
    u2 = (ix + 7.5f) * 1.0f / 256.0f;
    v2 = (iy + 15.5f) * 1.0f / 256.0f;

    vert.flags = PVR_CMD_VERTEX;
    vert.x = x1;
    vert.y = y1 + 16.0f * ys;
    vert.z = z1;
    vert.u = u1;
    vert.v = v2;
    vert.argb = PVR_PACK_COLOR(a, r, g, b);
    vert.oargb = 0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = x1;
    vert.y = y1;
    vert.u = u1;
    vert.v = v1;
    pvr_prim(&vert, sizeof(vert));

    vert.x = x1 + 8.0f * xs;
    vert.y = y1 + 16.0f * ys;
    vert.u = u2;
    vert.v = v2;
    pvr_prim(&vert, sizeof(vert));

    vert.flags = PVR_CMD_VERTEX_EOL;
    vert.x = x1 + 8.0f * xs;
    vert.y = y1;
    vert.u = u2;
    vert.v = v1;
    pvr_prim(&vert, sizeof(vert));
}

/* draw a string */
void draw_string(float x, float y, float z, float a, float r, float g, float b, char *str, float xs, float ys) {
    pvr_poly_cxt_t cxt;
    pvr_poly_hdr_t hdr;
    float orig_x = x;

    pvr_poly_cxt_txr(&cxt, PVR_LIST_TR_POLY, PVR_TXRFMT_ARGB4444, 256, 256, font_tex, PVR_FILTER_BILINEAR);
    pvr_poly_compile(&hdr, &cxt);
    pvr_prim(&hdr, sizeof(hdr));

    while(*str) {
        if(*str == '\n') {
            x = orig_x;
            y += 40;
            str++;
            continue;
        }

        draw_char(x, y, z, a, r, g, b, *str++, xs, ys);
        x += 8 * xs;
    }
}

/* base y coordinate */
int y = 0;

/* draw one frame */
void draw_frame(void) {
    pvr_wait_ready();
    pvr_scene_begin();

    pvr_list_begin(PVR_LIST_OP_POLY);
    draw_back();
    pvr_list_finish();

    pvr_list_begin(PVR_LIST_TR_POLY);

    /* 1720 and 480 are magic numbers directly related to the text scrolling
     * 1720 is enough room for the whole text to scroll from the bottom of
     * the screen to off the top.  31 lines * 40 pixels + 480 pixel high screen
     * 480 is the height of the screen (starts the text at the bottom)
     */
    draw_string(0, y % 1720 + 440, 3, 1, 1, 1, 1, data, 2, 2);

    pvr_list_finish();
    pvr_scene_finish();

    y--;
}

/* romdisk */
extern uint8 romdisk_boot[];
KOS_INIT_ROMDISK(romdisk_boot);

int main(void) {
    int done = 0;

    /* init kos  */
    pvr_init_defaults();

    /* init font */
    font_init();

    /* init background */
    back_init();

    /* init text */
    text_init();

    /* keep drawing frames until start is pressed */
    while(!done) {
        MAPLE_FOREACH_BEGIN(MAPLE_FUNC_CONTROLLER, cont_state_t, st)

        if(st->buttons & CONT_START)
            done = 1;

        MAPLE_FOREACH_END()

        draw_frame();
    }

    return 0;
}

just a standard example included with KOS. No SDL (just for you, bluecrab, haha)!
Still Thinking!~~
User avatar
ThePerfectK
Insane DCEmu
Insane DCEmu
Posts: 147
Joined: Thu Apr 27, 2006 10:15 am
Has thanked: 27 times
Been thanked: 35 times

Re: running compiled programs?

Post by ThePerfectK »

Ok, I downloaded and setup NullDC to try and debug why these programs won't start. To make things easier, I've gone back to the default hello world program. I'm compiling this source:

Code: Select all

/* KallistiOS ##version##

   hello.c
   (c)2001 Dan Potter
*/

#include <kos.h>

/* You can safely remove this line if you don't use a ROMDISK */
//extern uint8 romdisk[];

/* These macros tell KOS how to initialize itself. All of this initialization
   happens before main() gets called, and the shutdown happens afterwards. So
   you need to set any flags you want here. Here are some possibilities:

   INIT_NONE        -- don't do any auto init
   INIT_IRQ     -- Enable IRQs
   INIT_THD_PREEMPT -- Enable pre-emptive threading
   INIT_NET     -- Enable networking (doesn't imply lwIP!)
   INIT_MALLOCSTATS -- Enable a call to malloc_stats() right before shutdown

   You can OR any or all of those together. If you want to start out with
   the current KOS defaults, use INIT_DEFAULT (or leave it out entirely). */
KOS_INIT_FLAGS(INIT_DEFAULT | INIT_MALLOCSTATS);

/* And specify a romdisk, if you want one (or leave it out) */
//KOS_INIT_ROMDISK(romdisk);

/* Your program's main entry point */
int main(int argc, char **argv) {
    /* The requisite line */
    printf("\nHello world!\n\n");

    return 0;
}
using this makefile:

Code: Select all

#
# Basic KallistiOS skeleton / test program
# Copyright (C)2001-2004 Dan Potter
#   

# Put the filename of the output binary here
TARGET = hello.elf

#KOS_CFLAGS+=-I$(KOS_BASE)/../kos-ports/include/SDL 
#KOS_CFLAGS+= -I. -DDREAMCAST

# List all of your C files here, but change the extension to ".o"
# Include "romdisk.o" if you want a rom disk.
OBJS = hello.o romdisk.o

# If you define this, the Makefile.rules will create a romdisk.o for you
# from the named dir.
KOS_ROMDISK_DIR = romdisk

# The rm-elf step is to remove the target before building, to force the
# re-creation of the rom disk.
all: rm-elf $(TARGET)

include $(KOS_BASE)/Makefile.rules

clean:
	-rm -f $(TARGET) $(OBJS) romdisk.*

rm-elf:
	-rm -f $(TARGET) romdisk.*

$(TARGET): $(OBJS)
	kos-cc -o $(TARGET) $(OBJS)

run: $(TARGET)
	$(KOS_LOADER) $(TARGET)

dist:
	rm -f $(OBJS) romdisk.o romdisk.img
	$(KOS_STRIP) $(TARGET)
that is giving me Hello.elf. If I open NullDC and choose "load elf", nullDC crashes like this:

Image

if I choose "open elf", nullDC becomes unresponsive but doesn't crash, like so:

Image

Any ideas?
Still Thinking!~~
User avatar
SWAT
Insane DCEmu
Insane DCEmu
Posts: 191
Joined: Sat Jan 31, 2004 2:34 pm
Location: Russia/Novosibirsk
Has thanked: 1 time
Been thanked: 0
Contact:

Re: running compiled programs?

Post by SWAT »

ThePerfectK wrote: ...then my TV will report unusable signal. I've ordered a VGA cable to try it out on a normal monitor, maybe that'll help.
Yes, this bug in KOS has existed for several years and only recently corrected... VGA help you.
ThePerfectK wrote:Any ideas?
Run your CDI image instead ELF in nullDC, it's works fine. You can set up default image and run it automatically with emu.
But I'm using Data/Data type of images:

Code: Select all

cdi4dc.exe test.iso test.cdi -d
Image
User avatar
ThePerfectK
Insane DCEmu
Insane DCEmu
Posts: 147
Joined: Thu Apr 27, 2006 10:15 am
Has thanked: 27 times
Been thanked: 35 times

Re: running compiled programs?

Post by ThePerfectK »

generated the cdi image like you said, as a data/data image. Booted it in nullDC - no go. It just takes me to the dreamcast menu with play highlighted, no boot.

Is there something wrong with my mkisofs command? Am I not generating the iso correctly?
Still Thinking!~~
User avatar
SWAT
Insane DCEmu
Insane DCEmu
Posts: 191
Joined: Sat Jan 31, 2004 2:34 pm
Location: Russia/Novosibirsk
Has thanked: 1 time
Been thanked: 0
Contact:

Re: running compiled programs?

Post by SWAT »

ThePerfectK wrote: Is there something wrong with my mkisofs command? Am I not generating the iso correctly?
Maybe IP.BIN with different file name of 1ST_READ.BIN
Try set up directory instead of one file in mkisofs arguments.
Image
User avatar
ThePerfectK
Insane DCEmu
Insane DCEmu
Posts: 147
Joined: Thu Apr 27, 2006 10:15 am
Has thanked: 27 times
Been thanked: 35 times

Re: running compiled programs?

Post by ThePerfectK »

that did the trick! Much appreciated!

Man I'm excited!

Screenshot!
Still Thinking!~~
User avatar
SWAT
Insane DCEmu
Insane DCEmu
Posts: 191
Joined: Sat Jan 31, 2004 2:34 pm
Location: Russia/Novosibirsk
Has thanked: 1 time
Been thanked: 0
Contact:

Re: running compiled programs?

Post by SWAT »

Good luck :)
Image
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 366
Joined: Wed Mar 30, 2011 12:13 pm
Has thanked: 2 times
Been thanked: 0

Re: running compiled programs?

Post by RyoDC »

I would highly recommend against using of emulators, especially such one as nullDC. It is bad written, untested, unreliable piece of software on which you really shouldn't rely when testing your programs.
Better run your programs on real hardware.
Or test on others emulators, like makaron or demul.
User avatar
SiZiOUS
DC Developer
DC Developer
Posts: 404
Joined: Fri Mar 05, 2004 2:22 pm
Location: France
Has thanked: 27 times
Been thanked: 19 times
Contact:

Re: running compiled programs?

Post by SiZiOUS »

Yeah but nullDC is easy to use, so it can help to start the dev.
Glad to see that the problem of ThePerfectK is solved! :)
Post Reply