Skip to content

Commit

Permalink
WIP: compile for DOS (32bit protected mode) with DJGPP
Browse files Browse the repository at this point in the history
  • Loading branch information
miniupnp committed Jan 2, 2018
1 parent 1febded commit 1da332c
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 17 deletions.
2 changes: 1 addition & 1 deletion config.lib
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ make_compiler_cflags() {
exit 1
fi

if [ "$os" != "TOS" ] && [ $cc_version -lt 70 ] ; then
if [ "$os" != "TOS" ] && [ "$os" != "DOS" ] && [ $cc_version -lt 70 ] ; then
flags="$flags -ansi -pedantic"
fi
flags="$flags -fno-common"
Expand Down
6 changes: 5 additions & 1 deletion source.list
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ unit.c
#if TOS
video/video_atari.c
#else
video/video_sdl.c
#if DOS
video/video_dos.c
#else
video/video_sdl.c
#endif
#endif
#endif
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/* #define DUNE_DATA_DIR "/usr/local/share/opendune" */

#ifndef DUNE_DATA_DIR
#ifdef TOS
#if defined(TOS) || defined(DOS)
#define DUNE_DATA_DIR "DATA"
#else
#define DUNE_DATA_DIR "./data"
Expand Down Expand Up @@ -100,7 +100,7 @@ File_MakeCompleteFilename(char *buf, size_t len, enum SearchDirectory dir, const
int j;
int i = 0;

#ifdef TOS
#if defined(TOS) || defined(DOS)
if (dir == SEARCHDIR_GLOBAL_DATA_DIR || dir == SEARCHDIR_CAMPAIGN_DIR) {
/* Note: campaign specific data directory not implemented. */
i = snprintf(buf, len, "%s\\%s", g_dune_data_dir, filename);
Expand Down Expand Up @@ -514,10 +514,10 @@ bool File_Init(void)
PathAppend(buf, TEXT("OpenDUNE"));
strncpy(g_personal_data_dir, buf, sizeof(g_personal_data_dir));
}
#elif defined(TOS)
#elif defined(TOS) || defined(DOS)
(void)homedir;
strcpy(g_personal_data_dir, "SAVES");
#else /* _WIN32 / TOS*/
#else /* _WIN32 / TOS / DOS */
/* ~/.config/opendune (Linux) ~/Library/Application Support/OpenDUNE (Mac OS X) */
homedir = getenv("HOME");
if (homedir == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/inifile.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool Load_IniFile(void)
PathAppend(path, TEXT("OpenDUNE\\opendune.ini"));
f = fopen(path, "rb");
}
#elif !defined(TOS) /* _WIN32 */
#elif !defined(TOS) && !defined(DOS) /* _WIN32 */
char path[PATH_MAX];
char * homeDir;
homeDir = getenv("HOME");
Expand Down
11 changes: 10 additions & 1 deletion src/opendune.c
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ int main(int argc, char **argv)
if (err != NULL) _dup2(_fileno(err), _fileno(stderr));
if (out != NULL) _dup2(_fileno(out), _fileno(stdout));
FreeConsole();
#endif
#endif /* _WIN32 */
#ifdef TOS
(void)Cconws(window_caption);
(void)Cconws("\r\nrevision: ");
Expand All @@ -1236,7 +1236,16 @@ int main(int argc, char **argv)
if(atexit(exit_handler) != 0) {
Error("atexit() failed\n");
}
#endif /* TOS */
#ifdef DOS
/* open log files and set buffering mode */
g_errlog = fopen("error.log", "w");
if(g_errlog != NULL) setvbuf(g_errlog, NULL, _IONBF, 0);
#ifdef _DEBUG
g_outlog = fopen("output.log", "w");
if(g_outlog != NULL) setvbuf(g_outlog, NULL, _IOLBF, 0);
#endif
#endif /* DOS */
CrashLog_Init();

VARIABLE_NOT_USED(argc);
Expand Down
2 changes: 1 addition & 1 deletion src/os/endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define __BIG_ENDIAN 4321
#endif /* __BIG_ENDIAN */
#define __BYTE_ORDER __LITTLE_ENDIAN
#elif defined(__APPLE__) || defined(__FreeBSD__)
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__DJGPP__)
#include <machine/endian.h>
#if !defined(__BYTE_ORDER)
#define __BYTE_ORDER BYTE_ORDER
Expand Down
8 changes: 4 additions & 4 deletions src/os/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "error.h"

#ifdef TOS
#if defined(TOS) || defined(DOS)
bool g_consoleActive = true;
FILE * g_errlog = NULL;
#ifdef _DEBUG
Expand All @@ -20,7 +20,7 @@ void Error(const char *format, ...) {
va_list ap;

va_start(ap, format);
#ifdef TOS
#if defined(TOS) || defined(DOS)
if(g_errlog) vfprintf(g_errlog, format, ap);
if(g_consoleActive) vfprintf(stderr, format, ap);
#else
Expand All @@ -33,7 +33,7 @@ void Warning(const char *format, ...) {
va_list ap;

va_start(ap, format);
#ifdef TOS
#if defined(TOS) || defined(DOS)
if(g_errlog) vfprintf(g_errlog, format, ap);
if(g_consoleActive) vfprintf(stderr, format, ap);
#else
Expand All @@ -47,7 +47,7 @@ void Debug(const char *format, ...) {
va_list ap;

va_start(ap, format);
#ifdef TOS
#if defined(TOS) || defined(DOS)
if(g_outlog) vfprintf(g_outlog, format, ap);
if(g_consoleActive) vfprintf(stdout, format, ap);
#else
Expand Down
4 changes: 2 additions & 2 deletions src/os/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef OS_ERROR_H
#define OS_ERROR_H

#ifdef TOS
#if defined(TOS) || defined(DOS)
#include <stdio.h>
#endif

Expand All @@ -15,7 +15,7 @@ extern void Debug(const char *format, ...);
#define Debug(...)
#endif

#ifdef TOS
#if defined(TOS) || defined(DOS)
extern bool g_consoleActive;
extern FILE * g_errlog;
#ifdef _DEBUG
Expand Down
4 changes: 2 additions & 2 deletions src/os/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@

#if !defined(__MINGW32__) && defined(__GNUC__) && !defined(snprintf)
/* (v)snprintf is in fact C99, but we like to use it over (v)sprintf for the obvious reasons */
#if !defined(__APPLE__) && !defined(TOS) && !defined(__FreeBSD__)
#if !defined(__APPLE__) && !defined(TOS) && !defined(__FreeBSD__) && !defined(__DJGPP__)
extern int snprintf (char *__restrict __s, size_t __maxlen, __const char *__restrict __format, ...) __THROW __attribute__ ((__format__ (__printf__, 3, 4)));
extern int vsnprintf (char *__restrict __s, size_t __maxlen, __const char *__restrict __format, va_list __arg) __THROW __attribute__ ((__format__ (__printf__, 3, 0)));
#endif /* __APPLE__ */
#endif /* __GCC__ */

#if !defined(__MINGW32__) && defined(__GNUC__) && !defined(strdup) && !defined(__APPLE__) && !defined(TOS) && !defined(__FreeBSD__)
#if !defined(__MINGW32__) && defined(__GNUC__) && !defined(strdup) && !defined(__APPLE__) && !defined(TOS) && !defined(__FreeBSD__) && !defined(__DJGPP__)
/* strdup is not ANSI-C, but our own implemention would only be slower */
extern char *strdup (__const char *__s);
#endif /* __GCC__ */
Expand Down
97 changes: 97 additions & 0 deletions src/video/video_dos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/** @file src/video/video_dos.c IBM PC VGA video driver */

#include <dpmi.h>
#include <pc.h>
#include <sys/nearptr.h>

#include "types.h"
#include "video.h"
#include "../gfx.h"
#include "../os/error.h"

/* mouse : */
static int s_mouse_x = SCREEN_WIDTH/2;
static int s_mouse_x_min = 0;
static int s_mouse_x_max = SCREEN_WIDTH-1;
static int s_mouse_y = SCREEN_HEIGHT/2;
static int s_mouse_y_min = 0;
static int s_mouse_y_max = SCREEN_HEIGHT-1;
static bool s_mouse_left_btn = false;
static bool s_mouse_right_btn = true;

bool Video_Init(int screen_magnification, VideoScaleFilter filter)
{
__dpmi_regs r;
(void)screen_magnification;
(void)filter;

/* enable access to 1st MB of memory */
if (__djgpp_nearptr_enable() == 0) {
Error("__djgpp_nearptr_enable() failed, cannot access VGA memory.\n");
return false;
}

g_consoleActive = false;
r.x.ax = 0x13; /* MCGA 13h mode */
__dpmi_int(0x10, &r);
return true;
}

void Video_Uninit(void)
{
__dpmi_regs r;

__djgpp_nearptr_disable();
r.x.ax = 0x03; /* text mode */
__dpmi_int(0x10, &r);
g_consoleActive = true;
return;
}

void Video_Tick(void)
{
}

void Video_SetPalette(void *palette, int from, int length)
{
uint8 *p = palette;

outportb(0x3C8, from);
while(length > 0) {
outportb(0x3C9, *p++);
outportb(0x3C9, *p++);
outportb(0x3C9, *p++);
length--;
}
}

void Video_Mouse_SetPosition(uint16 x, uint16 y)
{
s_mouse_x = x;
s_mouse_y = y;
}

void Video_Mouse_SetRegion(uint16 minX, uint16 maxX, uint16 minY, uint16 maxY)
{
s_mouse_x_min = minX;
s_mouse_x_max = maxX;
s_mouse_y_min = minY;
s_mouse_y_max = maxY;
}

void Video_SetOffset(uint16 offset)
{
/* set Start Address High/Low VGA registers */
outportb(0x3D4, 0x0C);
outportb(0x3D5, offset >> 8);
outportb(0x3D4, 0x0D);
outportb(0x3D5, offset);
}

void * Video_GetFrameBuffer(uint16 size)
{
(void)size;

/* VGA frame buffer */
return (void *)(0x000a0000 + __djgpp_conventional_base);
}

0 comments on commit 1da332c

Please sign in to comment.