Skip to content

Commit

Permalink
Added Source Code
Browse files Browse the repository at this point in the history
  • Loading branch information
matteusbeus committed Jul 10, 2022
1 parent 1ae4a4d commit e691f98
Show file tree
Hide file tree
Showing 16 changed files with 6,804 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
ifdef $(GENDEV)
ROOTDIR = $(GENDEV)
else
ROOTDIR = /opt/toolchains/sega
endif

LDSCRIPTSDIR = $(ROOTDIR)/ldscripts

BOOTBLOCKDIR = $(ROOTDIR)/bootblocks
#BOOTBLOCK = $(BOOTBLOCKDIR)/US_BOOT.BIN
BOOTBLOCK = $(BOOTBLOCKDIR)/EU_BOOT.BIN
#BOOTBLOCK = $(BOOTBLOCKDIR)/JP_BOOT.BIN

LIBPATH = -L$(ROOTDIR)/m68k-elf/lib -L$(ROOTDIR)/m68k-elf/lib/gcc/m68k-elf/4.6.2 -L$(ROOTDIR)/m68k-elf/m68k-elf/lib
INCPATH = -I. -I../include -I$(ROOTDIR)/m68k-elf/include -I$(ROOTDIR)/m68k-elf/m68k-elf/include

CCFLAGS = -m68000 -Wall -O1 -c -fomit-frame-pointer
HWFLAGS = -m68000 -Wall -O1 -c -fomit-frame-pointer
LDFLAGS = -T $(LDSCRIPTSDIR)/cd.ld -Wl,-Map=output.map -nostdlib
ASFLAGS = -m68000 --register-prefix-optional

PREFIX = $(ROOTDIR)/m68k-elf/bin/m68k-elf-
CC = $(PREFIX)gcc
AS = $(PREFIX)as
LD = $(PREFIX)ld
OBJC = $(PREFIX)objcopy

ASMZ80 = $(ROOTDIR)/bin/zasm
FLAGSZ80 = -vb2

DD = dd
RM = rm -rf

TARGET = CDModPlayer
LIBS = $(LIBPATH) -lpcm -lc -lgcc -lnosys
OBJS = crt0.o main.o module.o cdfh.o hw_md.o
FILES =

all: $(TARGET).bin

$(TARGET).bin: $(TARGET).elf
$(OBJC) -O binary $< temp.bin
$(DD) if=temp.bin of=$@ bs=2048 conv=sync

$(TARGET).elf: $(OBJS) $(FILES)
$(CC) $(LDFLAGS) $(OBJS) $(LIBS) $(FILES) -o $(TARGET).elf

%.o80: %.s80
$(ASMZ80) $(FLAGSZ80) -o $@ $<

module.o: module.c
$(CC) $(HWFLAGS) -DPROC_PATTERNS $(INCPATH) $< -o $@

%.o: %.c
$(CC) $(CCFLAGS) $(INCPATH) $< -o $@

%.o: %.s
$(AS) $(ASFLAGS) $(INCPATH) $< -o $@

cd: $(TARGET).bin
mkdir -p cd
mkdir -p cd/MODs
cp $(TARGET).bin cd/APP.BIN
cp -r MODs/* cd/MODs/
genisoimage -sysid "SEGA SEGACD" -volid "CDMODPLAYER" -generic-boot $(BOOTBLOCK) -full-iso9660-filenames -o $(TARGET).iso cd

clean:
$(RM) *.o *.o80 *.bin *.elf *.map *.log *.iso cd
199 changes: 199 additions & 0 deletions cdfh.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hw_md.h"
#include "cdfh.h"

//----------------------------------------------------------------------
// SegaCD File Handler - by Chilly Willy
// Inspired by the MikMod READER structure.
//----------------------------------------------------------------------

static uint8_t cd_Eof(CDFileHandle_t *handle)
{
if (!handle)
return 1;

if (handle->pos >= handle->length)
return 1;

return 0;
}

static int32_t cd_Read(CDFileHandle_t *handle, void *ptr, int32_t size)
{
int32_t pos, blk, len, read = 0;
uint8_t *dst = ptr;

if (!handle)
return 0;

while (size != 0)
{
if (handle->Eof(handle))
return read;

pos = handle->pos;
blk = (pos >> 11) + handle->offset;
if (handle->block != blk)
{
read_cd(blk, 1, (void *)0x6800);
handle->block = blk;
}

len = 0x800 - (pos & 0x7FF);
if (len > size)
len = size;
if (len > (handle->length - pos))
len = (handle->length - pos);

memcpy(dst, (char *)0x6800 + (pos & 0x7FF), len);

handle->pos += len;
dst += len;
read += len;
size -= len;
}

return read;
}

static uint8_t cd_Get(CDFileHandle_t *handle)
{
int32_t pos, blk;

if (handle->Eof(handle))
return 0;

pos = handle->pos;
blk = (pos >> 11) + handle->offset;
if (handle->block != blk)
{
read_cd(blk, 1, (void *)0x6800);
handle->block = blk;
}

handle->pos++;
return ((uint8_t*)0x6800)[pos & 0x7FF];
}

static int32_t cd_Seek(CDFileHandle_t *handle, int32_t offset, int32_t whence)
{
int32_t pos;

if (!handle)
return -1;

pos = handle->pos;
switch(whence)
{
case SEEK_CUR:
pos += offset;
break;
case SEEK_SET:
pos = offset;
break;
case SEEK_END:
pos = handle->length - offset - 1;
break;
}
if (pos < 0)
handle->pos = 0;
else if (pos > handle->length)
handle->pos = handle->length;
else
handle->pos = pos;

return handle->pos;
}

static int32_t cd_Tell(CDFileHandle_t *handle)
{
return handle ? handle->pos : 0;
}


CDFileHandle_t *cd_handle_from_offset(int32_t offset, int32_t length)
{
CDFileHandle_t *handle = (CDFileHandle_t*)malloc(sizeof(CDFileHandle_t));
if (handle)
{
handle->Eof = &cd_Eof;
handle->Read = &cd_Read;
handle->Get = &cd_Get;
handle->Seek = &cd_Seek;
handle->Tell = &cd_Tell;
handle->offset = offset;
handle->length = length;
handle->block = -1; // nothing read yet
handle->pos = 0;
}
return handle;
}

CDFileHandle_t *cd_handle_from_name(char *name)
{
int32_t i;
char temp[256];

CDFileHandle_t *handle = (CDFileHandle_t*)malloc(sizeof(CDFileHandle_t));
if (handle)
{
handle->Eof = &cd_Eof;
handle->Read = &cd_Read;
handle->Get = &cd_Get;
handle->Seek = &cd_Seek;
handle->Tell = &cd_Tell;

i = strlen(name);
while (i && (name[i] != '/'))
i--;
if (name[i] == '/')
{
if (i)
{
strncpy(temp, name, i);
temp[i] = 0;
}
else
{
strcpy(temp, "/");
}
if (set_cwd(temp) < 0)
{
// error setting working directory
free(handle);
return NULL;
}
strncpy(temp, &name[i+1], 255);
temp[255] = 0;
}
else
{
strncpy(temp, name, 255);
temp[255] = 0;
}

if (find_dir_entry(temp) < 0)
{
// error finding entry
free(handle);
return NULL;
}

handle->offset = global_vars->DENTRY_OFFSET;
handle->length = global_vars->DENTRY_LENGTH;
handle->block = -1; // nothing read yet
handle->pos = 0;
}
return handle;
}

void delete_cd_handle(CDFileHandle_t *handle)
{
if (handle)
free(handle);
}

30 changes: 30 additions & 0 deletions cdfh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef _CDFH_H
#define _CDFH_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct CDFileHandle {
int32_t (*Seek)(struct CDFileHandle *handle, int32_t offset, int32_t whence);
int32_t (*Tell)(struct CDFileHandle *handle);
int32_t (*Read)(struct CDFileHandle *handle, void *ptr, int32_t size);
uint8_t (*Get)(struct CDFileHandle *handle);
uint8_t (*Eof)(struct CDFileHandle *handle);
int32_t offset; // start block of file
int32_t length; // length of file
int32_t block; // current block in buffer
int32_t pos; // current position in file
} CDFileHandle_t;

extern CDFileHandle_t *cd_handle_from_name(char *name);
extern CDFileHandle_t *cd_handle_from_offset(int32_t offset, int32_t length);
extern void delete_cd_handle(CDFileHandle_t *handle);

#ifdef __cplusplus
}
#endif

#endif
45 changes: 45 additions & 0 deletions crt0.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
| SEGA CD support code
| by Chilly Willy

.text

| CD startup at 0x8000

.global _start
_start:
move #0x2700,sr /* disable interrupts */

| Clear BSS
lea __bss_start,a0
lea __bss_end,a1
moveq #0,d0
1:
move.l d0,(a0)+
cmpa.l a0,a1
bhi.b 1b

move.l sp,__stack_save /* save BIOS stack pointer */
lea __stack,a0
movea.l a0,sp /* set stack pointer to top of Program RAM */
link.w a6,#-8 /* set up initial stack frame */

jsr init_hardware /* initialize the console hardware */

jsr __INIT_SECTION__ /* do all program initializers */
jsr main /* call program main() */
jsr __FINI_SECTION__ /* do all program finishers */

movea.l __stack_save,sp /* restore BIOS stack pointer */
moveq #0,d0
rts


.data

.align 4

__stack_save:
.long 0

.text

5 changes: 5 additions & 0 deletions files.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#define NUM_FILES 5

extern char *fileName[NUM_FILES];
extern int fileSize[NUM_FILES];
extern int filePtr[NUM_FILES];
4 changes: 4 additions & 0 deletions files.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.text

.align 4

Loading

0 comments on commit e691f98

Please sign in to comment.