muddle/ports/dos-like/dos-like.c

218 lines
4.9 KiB
C

/*
* Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "portme.h"
#include "story.h"
#include "state.h"
#include "interpreter.h"
#include "zork1.h"
//#include "czech.z3.h"
#pragma push_macro("bool")
#undef bool
#define DOS_IMPLEMENTATION
#include "dos.h"
#pragma pop_macro("bool")
// The F256 has 512k of RAM. We use everything except the lower 64k.
static uint8_t _RAM[1024 * (512 - 64)];
uint8_t portByteGet(uint32_t address) {
return _RAM[address];
}
void portByteSet(uint32_t address, uint8_t value) {
_RAM[address] = value;
}
char portCharGet(void) {
char c;
enum keycode_t key;
#if 0
static char playback[] = "open mailbox\ntake leaflet\nread leaflet\ndrop leaflet\n";
static uint32_t pointer = 0;
if (pointer < strlen(playback)) return playback[pointer++];
#endif
do {
c = *readchars();
key = *readkeys();
if (key == KEY_RETURN) c = 13;
if (key == KEY_BACK) c = 127;
if (key == KEY_DELETE) c = 8;
} while (c == 0);
return c;
}
void portCharGetPos(byte *x, byte *y) {
*x = wherex() + 1;
*y = wherey() + 1;
}
void portCharPrint(char c) {
char ch[2] = { 0, 0 };
if ((wherex() >= screenwidth()) || (c == '\n')) {
gotoxy(0, wherey() + 1);
while (wherey() >= screenheight()) {
memmove(((uint16_t*)screenbuffer()), ((uint16_t*)screenbuffer()) + screenwidth(), screenwidth() * (screenheight() - 1) * sizeof(uint16_t));
memset(((uint16_t*)screenbuffer()) + screenwidth() * (screenheight() - 1), 0, screenwidth() * sizeof(uint16_t));
gotoxy(0, wherey() - 1);
}
return;
}
ch[0] = c;
cputs(ch);
}
void portCharSetPos(byte x, byte y) {
gotoxy(x - 1, y - 1);
}
void portDie(char *fmt, ...) {
va_list ap;
printf("\n");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
exit(1);
}
bool portFileRestore(void) {
FILE *in;
bool ok = false;
in = fopen("save.dat", "rb");
if (in) {
ok = true;
// Read in PC.
ok &= (fread(&__state.pc, sizeof(__state.pc), 1, in) == 1);
// Read in SP.
ok &= (fread(&__state.sp, sizeof(__state.sp), 1, in) == 1);
// Read in BP.
ok &= (fread(&__state.bp, sizeof(__state.bp), 1, in) == 1);
// Read in dynamic game RAM.
ok &= (fread(_RAM, storyStaticMemoryBaseAddress(), 1, in) == 1);
// Read in stack.
ok &= (fread(__state.stack, sizeof(__state.stack[0]), __state.sp, in) == __state.sp);
fclose(in);
}
return ok;
}
bool portFileSave(void) {
FILE *out;
bool ok = false;
out = fopen("save.dat", "wb");
if (out) {
ok = true;
// Write out PC.
ok &= (fwrite(&__state.pc, sizeof(__state.pc), 1, out) == 1);
// Write out SP.
ok &= (fwrite(&__state.sp, sizeof(__state.sp), 1, out) == 1);
// Write out BP.
ok &= (fwrite(&__state.bp, sizeof(__state.bp), 1, out) == 1);
// Write out dynamic game RAM.
ok &= (fwrite(_RAM, storyStaticMemoryBaseAddress(), 1, out) == 1);
// Write out stack.
ok &= (fwrite(__state.stack, sizeof(__state.stack[0]), __state.sp, out) == __state.sp);
fclose(out);
}
return ok;
}
uint16_t portRandomGet(int16_t range) {
// If range is zero, randomize with "best" random seed.
// If range is negative, use the positive value to seed.
return 42;
}
void portStoryLoad(void) {
// For now, we just copy an embedded Zork 1 into RAM.
memcpy(_RAM, zork1, zork1_len);
//memcpy(_RAM, czech_z3, czech_z3_len);
}
uint16_t portWordGet(uint32_t address) {
return ((uint16_t)_RAM[address] << 8) | ((uint16_t)_RAM[address + 1]);
}
void portWordSet(uint32_t address, uint16_t value) {
_RAM[address] = (value >> 8) & 0xff; // MSB first.
_RAM[address + 1] = value & 0xff;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
setvideomode(videomode_80x25_9x16);
curson();
clrscr();
stateReset();
portStoryLoad();
opcodesSetup();
storySetup(80, 25);
interpreterRun();
return 0;
}