muddle/ports/f256/f256zip.c
2024-02-01 19:43:16 -06:00

200 lines
4.7 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.
*/
/*
* Z-Machine for the Foenix F256 computers.
*
* F256 machines are 65c02 based systems with 512k of banked RAM.
* As such, this ZIP is going to load the story entirely into "high"
* memory (above the 64k accessable to the CPU) and page things in
* and out as needed.
*
* To keep library code at a minimum, we'll also statically allocate
* as much as possible and do other "bad" things now frowned upon in
* modern development. :-)
*/
#include "f256.h"
#include "portme.h"
#include "story.h"
#include "memory.h"
#include "state.h"
#include "interpreter.h"
#define BASE_ADDRESS 0x10000
uint8_t portByteGet(uint16_t address) {
return FAR_PEEK(BASE_ADDRESS + address);
}
void portByteSet(uint16_t address, uint8_t value) {
FAR_POKE(BASE_ADDRESS + address, value);
}
void portCharPrint(char c) {
static char ch[2] = { 0, 0 };
ch[0] = c;
textPrint(ch);
}
void portDie(char *fmt, ...) {
exit(1);
}
bool portFileRestore(void) {
FILE *in;
bool ok = false;
uint32_t i;
byte b;
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.
for (i=0; i<storyStaticMemoryBaseAddress(); i++) {
ok &= (fread(&b, 1, 1, in) == 1);
ZPOKE(i, b);
}
// 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;
uint32_t i;
byte b;
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.
for (i=0; i<storyStaticMemoryBaseAddress(); i++) {
b = ZPEEK(i);
ok &= (fwrite(&b, 1, 1, out) == 1);
}
// Write out stack.
ok &= (fwrite(__state.stack, sizeof(__state.stack[0]), __state.sp, out) == __state.sp);
fclose(out);
}
return ok;
}
void portInput(uint32_t ramAddr, uint8_t length) {
uint8_t i;
char c;
for (i=0; i<length; i++) {
c = getchar();
ZPOKE(ramAddr, c);
if ((c == 13) || (c == 10)) break;
}
}
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.
if (range == 0) return 0;
if (range < 0) {
randomSeed(-range);
return 0;
}
return (randomRead() % range) + 1;
}
void portStoryLoad(void) {
// Later, we probably want to see if the user has a memory expansion
// installed. If they do, we can use it and then use the lower memory
// for graphics and sound.
// Currently no status bar.
ZPOKE(STORY_FLAG_V3, ZPEEK(STORY_FLAG_V3) | STORY_FLAG_V3_STATUS_LINE_NOT_AVAILABLE);
// Uncomment for status bar and window splitting.
//POKE(STORY_FLAG_V3, PEEK(STORY_FLAG_V3) & ~STORY_FLAG_V3_STATUS_LINE_NOT_AVAILABLE);
//POKE(STORY_FLAG_V3, PEEK(STORY_FLAG_V3) | STORY_FLAG_V3_SCREEN_SPLITTING);
}
uint16_t portWordGet(uint16_t address) {
return SWAP_UINT16(FAR_PEEKW(BASE_ADDRESS + address));
}
void portWordSet(uint16_t address, uint16_t value) {
FAR_POKEW(BASE_ADDRESS + address, SWAP_UINT16(value));
}
int main(void) {
f256Init();
textSetCursor(199);
stateReset();
portStoryLoad();
opcodesSetup();
storySetup();
interpreterRun();
return 0;
}